UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

163 lines 9.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const JwsToken_1 = require("./jws/JwsToken"); const ProtectionFormat_1 = require("../../keyStore/ProtectionFormat"); const CryptoProtocolError_1 = require("../CryptoProtocolError"); const JoseConstants_1 = require("./JoseConstants"); const JweToken_1 = require("./jwe/JweToken"); /** * Class to implement the JOSE protocol. */ class JoseProtocol { /** * Signs contents using the given private key reference. * * @param signingKeyReference Reference to the signing key. * @param payload to sign. * @param format of the final signature. * @param options used for the signature. These options override the options provided in the constructor. * @returns Signed payload in requested format. */ async sign(signingKeyReference, payload, format, options) { const jwsOptions = JwsToken_1.default.fromPayloadProtectionOptions(options); const token = new JwsToken_1.default(jwsOptions); const protocolFormat = this.getProtectionFormat(format); return JwsToken_1.default.toCryptoToken(protocolFormat, await token.sign(signingKeyReference, payload, protocolFormat), options); } /** * Verify the signature. * * @param validationKeys Public key to validate the signature. * @param payload that was signed * @param signature on payload * @param options used for the signature. These options override the options provided in the constructor. * @returns True if signature validated. */ async verify(validationKeys, _payload, signature, options) { const jwsOptions = JwsToken_1.default.fromPayloadProtectionOptions(options); const token = JwsToken_1.default.fromCryptoToken(signature, options); const result = await token.verify(validationKeys); return { result: result, reason: '' }; } /** * Encrypt content using the given public keys in JWK format. * The key type enforces the key encryption algorithm. * The options can override certain algorithm choices. * * @param recipients List of recipients' public keys. * @param payload to encrypt. * @param format of the final serialization. * @param options used for the encryption. These options override the options provided in the constructor. * @returns JweToken with encrypted payload. */ async encrypt(recipients, payload, format, options) { const jweOptions = JweToken_1.default.fromPayloadProtectionOptions(options); const token = new JweToken_1.default(jweOptions); const protocolFormat = this.getProtectionFormat(format); return JweToken_1.default.toCryptoToken(protocolFormat, await token.encrypt(recipients, payload.toString('utf8'), protocolFormat, jweOptions), options); } /** * Decrypt the content. * * @param decryptionKeyReference Reference to the decryption key. * @param token The crypto token to decrypt. * @param options used for the decryption. These options override the options provided in the constructor. * @returns Decrypted payload. */ async decrypt(decryptionKeyReference, token, options) { const cipher = JweToken_1.default.fromCryptoToken(token, options); cipher.options = JweToken_1.default.fromPayloadProtectionOptions(options); return await cipher.decrypt(decryptionKeyReference); } /** * Serialize a cryptographic token * @param token The crypto token to serialize. * @param format Specify the serialization format. If not specified, use default format. * @param options used for the decryption. These options override the options provided in the constructor. */ serialize(token, format, options) { const protocolFormat = this.getProtectionFormat(format); switch (protocolFormat) { case ProtectionFormat_1.ProtectionFormat.JwsFlatJson: case ProtectionFormat_1.ProtectionFormat.JwsCompactJson: case ProtectionFormat_1.ProtectionFormat.JwsGeneralJson: const signature = JwsToken_1.default.fromCryptoToken(token, options); return signature.serialize(protocolFormat); case ProtectionFormat_1.ProtectionFormat.JweFlatJson: case ProtectionFormat_1.ProtectionFormat.JweCompactJson: case ProtectionFormat_1.ProtectionFormat.JweGeneralJson: const cipher = JweToken_1.default.fromCryptoToken(token, options); return cipher.serialize(protocolFormat); default: throw new CryptoProtocolError_1.default(JoseConstants_1.default.Jose, `Serialization format '${format}' is not supported`); } } /** * Deserialize a cryptographic token * @param token The crypto token to deserialize. * @param format Specify the serialization format. If not specified, use default format. * @param options used for the decryption. These options override the options provided in the constructor. */ deserialize(token, format, options) { const protocolFormat = this.getProtectionFormat(format); switch (protocolFormat) { case ProtectionFormat_1.ProtectionFormat.JwsFlatJson: case ProtectionFormat_1.ProtectionFormat.JwsCompactJson: case ProtectionFormat_1.ProtectionFormat.JwsGeneralJson: const jwsProtectOptions = JwsToken_1.default.fromPayloadProtectionOptions(options); return JwsToken_1.default.toCryptoToken(protocolFormat, JwsToken_1.default.deserialize(token, jwsProtectOptions), options); case ProtectionFormat_1.ProtectionFormat.JweFlatJson: case ProtectionFormat_1.ProtectionFormat.JweCompactJson: case ProtectionFormat_1.ProtectionFormat.JweGeneralJson: const jweProtectOptions = JweToken_1.default.fromPayloadProtectionOptions(options); return JweToken_1.default.toCryptoToken(protocolFormat, JweToken_1.default.deserialize(token, jweProtectOptions), options); default: throw new CryptoProtocolError_1.default(JoseConstants_1.default.Jose, `Serialization format '${format}' is not supported`); } } /** * Deserialize a cryptographic token * @param token The crypto token to deserialize. * @param options used for the token. These options override the options provided in the constructor. */ static deserialize(token, options) { const parts = token.split('.'); const protocol = new JoseProtocol(); if (parts.length === 3) { const deserializationOptions = options ? JwsToken_1.default.fromPayloadProtectionOptions(options) : {}; return JwsToken_1.default.toCryptoToken(ProtectionFormat_1.ProtectionFormat.JwsCompactJson, JwsToken_1.default.deserialize(token, deserializationOptions), options); } else if (parts.length === 5) { const deserializationOptions = options ? JweToken_1.default.fromPayloadProtectionOptions(options) : {}; return JweToken_1.default.toCryptoToken(ProtectionFormat_1.ProtectionFormat.JweCompactJson, JweToken_1.default.deserialize(token, deserializationOptions), options); } const parsed = JSON.parse(token); if (parsed[JoseConstants_1.default.tokenSignatures] || parsed[JoseConstants_1.default.tokenSignature]) { const deserializationOptions = options ? JwsToken_1.default.fromPayloadProtectionOptions(options) : {}; return JwsToken_1.default.toCryptoToken(parsed[JoseConstants_1.default.tokenSignatures] ? ProtectionFormat_1.ProtectionFormat.JwsGeneralJson : ProtectionFormat_1.ProtectionFormat.JwsFlatJson, JwsToken_1.default.deserialize(token, deserializationOptions), options); } if (parsed[JoseConstants_1.default.tokenRecipients] || parsed[JoseConstants_1.default.tokenCiphertext]) { const deserializationOptions = options ? JweToken_1.default.fromPayloadProtectionOptions(options) : {}; return JweToken_1.default.toCryptoToken(parsed[JoseConstants_1.default.tokenRecipients] ? ProtectionFormat_1.ProtectionFormat.JweGeneralJson : ProtectionFormat_1.ProtectionFormat.JweFlatJson, JweToken_1.default.deserialize(token, deserializationOptions), options); } throw new CryptoProtocolError_1.default(JoseConstants_1.default.Jose, 'Unrecognised token to deserialize'); } // Map string to protection format getProtectionFormat(format) { switch (format.toLocaleLowerCase()) { case 'jwsflatjson': return ProtectionFormat_1.ProtectionFormat.JwsFlatJson; case 'jwscompactjson': return ProtectionFormat_1.ProtectionFormat.JwsCompactJson; case 'jwsgeneraljson': return ProtectionFormat_1.ProtectionFormat.JwsGeneralJson; case 'jweflatjson': return ProtectionFormat_1.ProtectionFormat.JweFlatJson; case 'jwecompactjson': return ProtectionFormat_1.ProtectionFormat.JweCompactJson; case 'jwegeneraljson': return ProtectionFormat_1.ProtectionFormat.JweGeneralJson; default: throw new CryptoProtocolError_1.default(JoseConstants_1.default.Jose, `Format '${format}' is not supported`); } } } exports.default = JoseProtocol; //# sourceMappingURL=JoseProtocol.js.map