Skip to content

Commit

Permalink
fix: more
Browse files Browse the repository at this point in the history
  • Loading branch information
auer-martin committed Oct 8, 2024
1 parent 18d684a commit 9967166
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 490 deletions.
6 changes: 6 additions & 0 deletions .changeset/cuddly-suns-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@protokoll/mdoc-client': patch
'@protokoll/mdoc-node': patch
---

fix: don't export everything
2 changes: 1 addition & 1 deletion packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export * from './u-base64.js';
export * from './u-hex.js';
export * from './u-misc.js';
export * from './u-request.js';
export * from './u-uint-8-array.js';
export * from './u-uint8-array.js';
export * from './u-valibot.js';
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,35 @@ export function stringToUint8Array(input: string): Uint8Array {
}
return uint8Array;
}

export function concatUint8Array(...buffers: Uint8Array[]): Uint8Array {
const size = buffers.reduce((acc, { length }) => acc + length, 0);
const buf = new Uint8Array(size);
let i = 0;
buffers.forEach(buffer => {
buf.set(buffer, i);
i += buffer.length;
});
return buf;
}

export function areEqualUint8Array(
buf1: Uint8Array,
buf2: Uint8Array
): boolean {
if (buf1 === buf2) {
return true;
}

if (buf1.byteLength !== buf2.byteLength) {
return false;
}

for (let i = 0; i < buf1.byteLength; i++) {
if (buf1[i] !== buf2[i]) {
return false;
}
}

return true;
}
3 changes: 2 additions & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@protokoll/core": "workspace:*"
"@protokoll/core": "workspace:*",
"valibot": "catalog:"
},
"devDependencies": {
"@protokoll/eslint-config": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/src/subtls-dsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const subtleDsa = (
case 'PS256':
case 'PS384':
case 'PS512':
// @ts-expect-error this should work
// @ts-expect-error this works
return { hash, name: 'RSA-PSS', saltLength: alg.slice(-3) >> 3 };
case 'RS256':
case 'RS384':
Expand Down
3 changes: 1 addition & 2 deletions packages/mdoc/mdoc-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"@jfromaniello/typedmap": "^1.4.0",
"@protokoll/core": "workspace:*",
"cbor-x": "^1.6.0",
"compare-versions": "^6.0.0",
"valibot": "catalog:"
"compare-versions": "^6.0.0"
},
"devDependencies": {
"@protokoll/eslint-config": "workspace:*",
Expand Down
12 changes: 6 additions & 6 deletions packages/mdoc/mdoc-client/src/cose/key/cose-key.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { TypedMap } from '@jfromaniello/typedmap';
import { uint8ArrayToString } from '@protokoll/core';
import type { JWK } from 'jose';
import { cborDecode, cborEncode } from '../../cbor/index.js';
import {
base64UrlToUint8Array,
concatUint8Array,
uint8ArrayToBase64Url,
} from '../../mdoc/u-base64.js';
import { concat } from '../../u-buffer.js';
uint8ArrayToString,
} from '@protokoll/core';
import type { JWK } from 'jose';
import { cborDecode, cborEncode } from '../../cbor/index.js';
import { Algorithms } from '../headers.js';
import { Curve } from './curve.js';
import type { KeyOps } from './key-ops.js';
Expand Down Expand Up @@ -184,7 +184,7 @@ export const COSEKeyToRAW = (
return decodedKey.get(-4) as Uint8Array;
}

return concat(
return concatUint8Array(
Uint8Array.from([0x04]),
decodedKey.get(-2) as Uint8Array,
decodedKey.get(-3) as Uint8Array
Expand Down
13 changes: 5 additions & 8 deletions packages/mdoc/mdoc-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ export { DeviceResponse } from './mdoc/model/device-response.js';
export { DeviceSignedDocument } from './mdoc/model/device-signed-document.js';
export { Document } from './mdoc/model/document.js';
export { IssuerSignedDocument } from './mdoc/model/issuer-signed-document.js';
export { MDoc } from './mdoc/model/mdoc.js';
export { MDoc, MDocStatus } from './mdoc/model/mdoc.js';
export { limitDisclosureToInputDescriptor } from './mdoc/model/pex-limit-disclosure.js';
export type { PresentationDefinition } from './mdoc/model/presentation-definition.js';
export type {
DiagnosticInformation,
ValidityInfo,
} from './mdoc/model/types.js';
export { parse, parseDeviceSigned, parseIssuerSigned } from './mdoc/parser.js';
export {
base64ToUint8Array,
base64UrlToUint8Array,
uint8ArrayToBase64,
uint8ArrayToBase64Url,
} from './mdoc/u-base64.js';
parseDeviceResponse,
parseDeviceSigned,
parseIssuerSigned,
} from './mdoc/parser.js';
export { Verifier } from './mdoc/verifier.js';
export { areEqual } from './u-buffer.js';
4 changes: 2 additions & 2 deletions packages/mdoc/mdoc-client/src/mdoc/issuer-signed-item.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { areEqualUint8Array } from '@protokoll/core';
import type { MdocContext, X509Context } from '../c-mdoc.js';
import { DataItem } from '../cbor/data-item.js';
import { cborEncode } from '../cbor/index.js';
import { areEqual } from '../u-buffer.js';
import type IssuerAuth from './model/issuer-auth.js';
import type { DigestAlgorithm } from './model/types.js';

Expand Down Expand Up @@ -80,7 +80,7 @@ export class IssuerSignedItem {
}
const expectedDigest = digests.get(this.digestID);
this.#isValid =
expectedDigest && areEqual(new Uint8Array(digest), expectedDigest);
expectedDigest && areEqualUint8Array(digest, expectedDigest);
return Boolean(this.#isValid);
}

Expand Down
88 changes: 57 additions & 31 deletions packages/mdoc/mdoc-client/src/mdoc/model/device-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { COSEKey, COSEKeyToRAW } from '../../cose/key/cose-key.js';
import { Mac0 } from '../../cose/mac0.js';
import { Sign1 } from '../../cose/sign1.js';
import { parse } from '../parser.js';
import { parseDeviceResponse } from '../parser.js';
import { calculateDeviceAutenticationBytes } from '../utils.js';
import { DeviceSignedDocument } from './device-signed-document.js';
import { MDoc } from './mdoc.js';
Expand Down Expand Up @@ -49,7 +49,7 @@ export class DeviceResponse {
*/
public static from(mdoc: MDoc | Uint8Array): DeviceResponse {
if (mdoc instanceof Uint8Array) {
return new DeviceResponse(parse(mdoc));
return new DeviceResponse(parseDeviceResponse(mdoc));
}
return new DeviceResponse(mdoc);
}
Expand Down Expand Up @@ -122,23 +122,39 @@ export class DeviceResponse {
* @param {string} verifierGeneratedNonce - The nonce Authorization Request parameter from the Authorization Request Object.
* @returns {DeviceResponse}
*/
public usingSessionTranscriptForOID4VP(
mdocGeneratedNonce: string,
clientId: string,
responseUri: string,
verifierGeneratedNonce: string
): DeviceResponse {
this.usingSessionTranscriptBytes(
cborEncode(
DataItem.fromData([
null, // deviceEngagementBytes
null, // eReaderKeyBytes
[mdocGeneratedNonce, clientId, responseUri, verifierGeneratedNonce],
])
)
);
public usingSessionTranscriptForOID4VP(input: {
mdocGeneratedNonce: string;
clientId: string;
responseUri: string;
verifierGeneratedNonce: string;
}): DeviceResponse {
const bytes = DeviceResponse.calculateSessionTranscriptForOID4VP(input);
this.usingSessionTranscriptBytes(bytes);
return this;
}

public static calculateSessionTranscriptForOID4VP(input: {
mdocGeneratedNonce: string;
clientId: string;
responseUri: string;
verifierGeneratedNonce: string;
}) {
const {
mdocGeneratedNonce,
clientId,
responseUri,
verifierGeneratedNonce,
} = input;

return cborEncode(
DataItem.fromData([
null, // deviceEngagementBytes
null, // eReaderKeyBytes
[mdocGeneratedNonce, clientId, responseUri, verifierGeneratedNonce],
])
);
}

/**
* Set the session transcript data to use for the device response as defined in ISO/IEC 18013-7 in Annex A (Web API), 2023 draft.
*
Expand All @@ -149,23 +165,33 @@ export class DeviceResponse {
* @param {Uint8Array} eReaderKeyBytes - The reader ephemeral public key as a COSE Key, encoded as a Tagged 24 cbor
* @returns {DeviceResponse}
*/
public usingSessionTranscriptForWebAPI(
deviceEngagementBytes: Uint8Array,
readerEngagementBytes: Uint8Array,
eReaderKeyBytes: Uint8Array
): DeviceResponse {
this.usingSessionTranscriptBytes(
cborEncode(
DataItem.fromData([
new DataItem({ buffer: deviceEngagementBytes }),
new DataItem({ buffer: eReaderKeyBytes }),
readerEngagementBytes,
])
)
);
public usingSessionTranscriptForWebAPI(input: {
deviceEngagementBytes: Uint8Array;
readerEngagementBytes: Uint8Array;
eReaderKeyBytes: Uint8Array;
}): DeviceResponse {
const bytes = DeviceResponse.calculateSessionTranscriptForWebApi(input);
this.usingSessionTranscriptBytes(bytes);
return this;
}

public static calculateSessionTranscriptForWebApi(input: {
deviceEngagementBytes: Uint8Array;
readerEngagementBytes: Uint8Array;
eReaderKeyBytes: Uint8Array;
}) {
const { deviceEngagementBytes, eReaderKeyBytes, readerEngagementBytes } =
input;

return cborEncode(
DataItem.fromData([
new DataItem({ buffer: deviceEngagementBytes }),
new DataItem({ buffer: eReaderKeyBytes }),
readerEngagementBytes,
])
);
}

/**
* Add a namespace to the device response.
*
Expand Down
8 changes: 4 additions & 4 deletions packages/mdoc/mdoc-client/src/mdoc/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type DeviceAuth =

export interface DeviceSigned {
deviceAuth: DeviceAuth;
nameSpaces: Record<string, Record<string, any>>;
nameSpaces: Record<string, Record<string, unknown>>;
}

export type RawIndexedDataItem = IssuerSignedDataItem[];
Expand Down Expand Up @@ -56,14 +56,14 @@ export interface DiagnosticInformation {
attributes: {
ns: string;
id: string;
value: any;
value: unknown;
isValid: boolean;
matchCertificate?: boolean;
}[];
deviceAttributes: {
ns: string;
id: string;
value: any;
value: unknown;
}[];
issuerCertificate?: {
subjectName: string;
Expand Down Expand Up @@ -96,7 +96,7 @@ export interface DiagnosticInformation {

export interface DeviceKeyInfo {
deviceKey: Map<number, number | Uint8Array>;
[key: string]: any;
[key: string]: unknown;
}

export interface MSO {
Expand Down
4 changes: 2 additions & 2 deletions packages/mdoc/mdoc-client/src/mdoc/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const mapIssuerNameSpaces = (namespace: RawNameSpaces): IssuerNameSpaces => {
);
};

const mapDeviceNameSpaces = (namespace: Map<string, Map<string, any>>) => {
const mapDeviceNameSpaces = (namespace: Map<string, Map<string, unknown>>) => {
const entries = Array.from(namespace.entries()).map(([ns, attrs]) => {
return [ns, Object.fromEntries(attrs.entries())];
});
Expand Down Expand Up @@ -168,7 +168,7 @@ export const parseDeviceSigned = (
* @param encoded - The cbor encoded mdoc
* @returns {Promise<MDoc>} - The parsed device response
*/
export const parse = (encoded: Uint8Array): MDoc => {
export const parseDeviceResponse = (encoded: Uint8Array): MDoc => {
let deviceResponse;
try {
deviceResponse = cborDecode(encoded) as Map<string, any>;
Expand Down
Loading

0 comments on commit 9967166

Please sign in to comment.