UNPKG

@ldclabs/cose-ts

Version:

Implemented Keys, Algorithms (RFC9053), COSE (RFC9052) and CWT (RFC8392) in TypeScript.

72 lines 2.51 kB
// (c) 2023-present, LDC Labs. All rights reserved. // See the file LICENSE for licensing terms. import * as iana from './iana'; import { KVMap, assertBytes, assertIntOrText } from './map'; import { decodeCBOR, encodeCBOR } from './utils'; // Key implements algorithms and key objects for COSE as defined in RFC9052 and RFC9053. // https://datatracker.ietf.org/doc/html/rfc9052#name-key-objects. // https://datatracker.ietf.org/doc/html/rfc9053. export class Key extends KVMap { static fromBytes(data) { return new Key(decodeCBOR(data)); } constructor(kv = new Map()) { super(kv); } get kty() { return this.getType(iana.KeyParameterKty, assertIntOrText, 'kty'); } set kty(kty) { this.setParam(iana.KeyParameterKty, assertIntOrText(kty, 'kty')); } get kid() { return this.getBytes(iana.KeyParameterKid, 'kid'); } set kid(kid) { this.setParam(iana.KeyParameterKid, assertBytes(kid, 'kid')); } get alg() { return this.getType(iana.KeyParameterAlg, assertIntOrText, 'alg'); } set alg(alg) { this.setParam(iana.KeyParameterAlg, assertIntOrText(alg, 'alg')); } get ops() { return this.getArray(iana.KeyParameterKeyOps, assertIntOrText, 'ops'); } set ops(ops) { if (!Array.isArray(ops)) { throw new TypeError('ops must be an array'); } ops.forEach((op) => assertIntOrText(op, 'ops')); this.setParam(iana.KeyParameterKeyOps, ops); } get baseIV() { return this.getBytes(iana.KeyParameterBaseIV, 'Base IV'); } set baseIV(iv) { this.setParam(iana.KeyParameterBaseIV, assertBytes(iv, 'Base IV')); } // getKid gets the kid parameter with CBOR decoding. getKid() { return decodeCBOR(this.getBytes(iana.KeyParameterKid, 'kid')); } // setKid sets the kid parameter with CBOR encoding. setKid(kid) { this.setParam(iana.KeyParameterKid, encodeCBOR(kid)); return this; } getSecret() { switch (this.kty) { case iana.KeyTypeOKP: return this.getBytes(iana.OKPKeyParameterD, 'k'); case iana.KeyTypeEC2: return this.getBytes(iana.EC2KeyParameterD, 'd'); case iana.KeyTypeSymmetric: return this.getBytes(iana.SymmetricKeyParameterK, 'k'); default: throw new Error('unsupported key type'); } } } //# sourceMappingURL=key.js.map