Skip to content

Commit

Permalink
Use cbor-x instead of cbor-js for CBOR encoding/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
cyraxx committed Jul 26, 2024
1 parent 44dda51 commit 6a24707
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 22 deletions.
147 changes: 134 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"devDependencies": {
"@eslint/js": "^9.7.0",
"@ianvs/prettier-plugin-sort-imports": "^4.3.1",
"@types/cbor-js": "^0.1.1",
"@types/eslint__js": "^8.42.3",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.11",
Expand All @@ -53,7 +52,7 @@
},
"dependencies": {
"@peculiar/x509": "^1.11.0",
"cbor-js": "^0.1.0",
"cbor-x": "^1.5.9",
"crc-32": "^1.2.2",
"pkijs": "^3.2.1"
},
Expand Down
9 changes: 7 additions & 2 deletions src/cose/SigStructure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cbor from 'cbor-js';
import * as cbor from 'cbor-x';

export class SigStructure {
public readonly externalAAD: Uint8Array = new Uint8Array(0);
Expand All @@ -10,6 +10,11 @@ export class SigStructure {
) {}

public encode(): Uint8Array {
return new Uint8Array(cbor.encode([this.context, this.protectedBucket, this.externalAAD, this.payload]));
return new cbor.Encoder({ tagUint8Array: false }).encode([
this.context,
this.protectedBucket,
this.externalAAD,
this.payload,
]);
}
}
4 changes: 2 additions & 2 deletions src/cose/Signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
SubjectKeyIdentifierExtension,
X509Certificate,
} from '@peculiar/x509';
import cbor from 'cbor-js';
import * as cbor from 'cbor-x';
import { PKIStatus, SignedData, TimeStampResp, TSTInfo } from 'pkijs';
import { Crypto, ECDSANamedCurve, HashAlgorithm, SigningAlgorithm } from '../crypto';
import * as JUMBF from '../jumbf';
Expand All @@ -35,7 +35,7 @@ export class Signature {

let protectedBucket: ProtectedBucket | undefined;
try {
protectedBucket = cbor.decode(BinaryHelper.toArrayBuffer(rawContent[0])) as ProtectedBucket;
protectedBucket = cbor.decode(rawContent[0]) as ProtectedBucket;
} catch {
/* empty */
}
Expand Down
10 changes: 7 additions & 3 deletions src/jumbf/CBORBox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cbor from 'cbor-js';
import { BinaryHelper } from '../util';
import * as cbor from 'cbor-x';
import { Box } from './Box';

export class CBORBox extends Box {
Expand All @@ -14,7 +13,12 @@ export class CBORBox extends Box {
public parse(buf: Uint8Array) {
this.rawContent = buf;
try {
this.content = cbor.decode(BinaryHelper.toArrayBuffer(buf));
this.content = cbor.decode(buf);

// Ignore unknown CBOR tags
if (this.content instanceof cbor.Tag) {
this.content = this.content.value;
}
} catch {
// TODO This needs to be properly reported as a validation error
throw new Error('CBORBox: Invalid CBOR data');
Expand Down

0 comments on commit 6a24707

Please sign in to comment.