UNPKG

cose-kit

Version:

This is an early prototype of a RFC8152 COSE library for node.js.

98 lines (97 loc) 3.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Mac0 = void 0; const verify_js_1 = __importDefault(require("#runtime/verify.js")); const sign_js_1 = __importDefault(require("#runtime/sign.js")); const cbor_js_1 = require("../cbor.js"); const WithHeaders_js_1 = require("./WithHeaders.js"); const headers_js_1 = require("../headers.js"); const buffer_utils_js_1 = require("../lib/buffer_utils.js"); class Mac0 extends WithHeaders_js_1.WithHeaders { constructor(protectedHeaders, unprotectedHeaders, payload, tag) { super(protectedHeaders, unprotectedHeaders); this.payload = payload; this.tag = tag; } static createMAC0(protectedHeaders, applicationHeaders, payload) { return cbor_js_1.encoder.encode([ 'MAC0', protectedHeaders, applicationHeaders, payload, ]); } getContentForEncoding() { return [ this.encodedProtectedHeaders, this.unprotectedHeaders, this.payload, this.tag, ]; } encode() { return cbor_js_1.encoder.encode(this); } async verify(key, externalAAD = new Uint8Array(), detachedPayload) { if (!key) { throw new Error('key not found'); } const mac0Structure = Mac0.createMAC0(this.encodedProtectedHeaders || new Uint8Array(), externalAAD, detachedPayload !== null && detachedPayload !== void 0 ? detachedPayload : this.payload); if (!this.algName) { throw new Error('unknown algorithm: ' + this.alg); } return (0, verify_js_1.default)(this.algName, key, this.tag, mac0Structure); } get alg() { return this.protectedHeaders.get(headers_js_1.headers.alg) || this.unprotectedHeaders.get(headers_js_1.headers.alg); } get algName() { var _a; return this.alg ? (_a = headers_js_1.macAlgs.get(this.alg)) === null || _a === void 0 ? void 0 : _a.name : undefined; } hasSupportedAlg() { return !!this.algName; } areEqual(mac0) { return (0, buffer_utils_js_1.areEqual)(this.tag, mac0.tag); } static async create(protectedHeaders, unprotectedHeaders, payload, key) { const { alg } = protectedHeaders; const encodedProtectedHeaders = cbor_js_1.encoder.encode(new Map(Object.entries(protectedHeaders).map(([k, v]) => { if (k === 'alg') { v = headers_js_1.macAlgsToValue.get(v); } else if (typeof v === 'string') { v = (0, buffer_utils_js_1.fromUTF8)(v); } return [headers_js_1.headers[k], v]; }))); const unprotectedHeadersMap = new Map(Object.entries(unprotectedHeaders || {}).map(([k, v]) => { if (typeof v === 'string') { v = (0, buffer_utils_js_1.fromUTF8)(v); } return [headers_js_1.headers[k], v]; })); const toBeSigned = Mac0.createMAC0(encodedProtectedHeaders, new Uint8Array(), payload); if (!alg) { throw new Error('The alg header must be set.'); } const tag = await (0, sign_js_1.default)(alg, key, toBeSigned); return new Mac0(encodedProtectedHeaders, unprotectedHeadersMap, payload, tag); } } exports.Mac0 = Mac0; (0, cbor_js_1.addExtension)({ Class: Mac0, tag: 17, encode(instance, encodeFn) { return encodeFn(instance.getContentForEncoding()); }, decode: (data) => { return new Mac0(data[0], data[1], data[2], data[3]); } });