Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: calcuate sd_hash in kb JWT #117

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/core-example/kb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { createSignerVerifier, digest, generateSalt } from './utils';
aud: 'https://example.com',
nonce: '1234',
custom: 'data',
sd_hash: '1234',
};

const encodedSdjwt = await sdjwt.issue(claims, disclosureFrame);
Expand Down
64 changes: 58 additions & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { SDJWTException } from '@sd-jwt/utils';
import { SDJWTException, Uint8ArrayToBase64Url } from '@sd-jwt/utils';
import { Jwt } from './jwt';
import { KBJwt } from './kbjwt';
import { SDJwt, pack } from './sdjwt';
import {
DisclosureFrame,
Hasher,
KBOptions,
KB_JWT_TYP,
SDJWTCompact,
SDJWTConfig,
SD_JWT_TYP,
} from '@sd-jwt/types';
import { getSDAlgAndPayload } from '@sd-jwt/decode';

export * from './sdjwt';
export * from './kbjwt';
Expand All @@ -27,20 +29,24 @@
}
}

private async createKBJwt(options: KBOptions): Promise<KBJwt> {
private async createKBJwt(
options: KBOptions,
sdHash: string,
): Promise<KBJwt> {
if (!this.userConfig.kbSigner) {
throw new SDJWTException('Key Binding Signer not found');
}
if (!this.userConfig.kbSignAlg) {
throw new SDJWTException('Key Binding sign algorithm not specified');
}

const { payload } = options;
const kbJwt = new KBJwt({
header: {
typ: KB_JWT_TYP,
alg: this.userConfig.kbSignAlg,
},
payload,
payload: { ...payload, sd_hash: sdHash },
});

await kbJwt.sign(this.userConfig.kbSigner);
Expand Down Expand Up @@ -127,9 +133,24 @@
const hasher = this.userConfig.hasher;

const sdjwt = await SDJwt.fromEncode(encodedSDJwt, hasher);
const kbJwt = options?.kb ? await this.createKBJwt(options.kb) : undefined;
sdjwt.kbJwt = kbJwt;

if (!sdjwt.jwt?.payload) throw new SDJWTException('Payload not found');
const presentSdJwtWithoutKb = await sdjwt.present(
presentationKeys.sort(),
hasher,
);

if (!options?.kb) {
return presentSdJwtWithoutKb;
}

const sdHashStr = await this.calculateSDHash(
presentSdJwtWithoutKb,
sdjwt,
hasher,
);

sdjwt.kbJwt = await this.createKBJwt(options.kb, sdHashStr);
return sdjwt.present(presentationKeys.sort(), hasher);
}

Expand All @@ -147,7 +168,7 @@
const hasher = this.userConfig.hasher;

const sdjwt = await SDJwt.fromEncode(encodedSDJwt, hasher);
if (!sdjwt.jwt) {
if (!sdjwt.jwt || !sdjwt.jwt.payload) {
throw new SDJWTException('Invalid SD JWT');
}
const { payload, header } = await this.validate(encodedSDJwt);
Expand All @@ -173,9 +194,40 @@
throw new SDJWTException('Key Binding Verifier not found');
}
const kb = await sdjwt.kbJwt.verify(this.userConfig.kbVerifier);
const sdHashfromKb = kb.payload.sd_hash;
const sdjwtWithoutKb = new SDJwt({
jwt: sdjwt.jwt,
disclosures: sdjwt.disclosures,
});

const presentSdJwtWithoutKb = sdjwtWithoutKb.encodeSDJwt();
const sdHashStr = await this.calculateSDHash(
presentSdJwtWithoutKb,
sdjwt,
hasher,
);

if (sdHashStr !== sdHashfromKb) {
throw new SDJWTException('Invalid sd_hash in Key Binding JWT');

Check warning on line 211 in packages/core/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/index.ts#L211

Added line #L211 was not covered by tests
}

return { payload, header, kb };
}

private async calculateSDHash(
presentSdJwtWithoutKb: string,
sdjwt: SDJwt,
hasher: Hasher,
) {
if (!sdjwt.jwt || !sdjwt.jwt.payload) {
throw new SDJWTException('Invalid SD JWT');
}

Check warning on line 224 in packages/core/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/index.ts#L223-L224

Added lines #L223 - L224 were not covered by tests
const { _sd_alg } = getSDAlgAndPayload(sdjwt.jwt.payload);
const sdHash = await hasher(presentSdJwtWithoutKb, _sd_alg);
const sdHashStr = Uint8ArrayToBase64Url(sdHash);
return sdHashStr;
}

// This function is for validating the SD JWT
// Just checking signature and return its the claims
public async validate(encodedSDJwt: string) {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/sdjwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import {
DisclosureFrame,
Hasher,
HasherAndAlg,
KBOptions,
KB_JWT_TYP,
SDJWTCompact,
SD_DECOY,
SD_DIGEST,
SD_LIST_KEY,
SD_SEPARATOR,
SaltGenerator,
Signer,
kbHeader,
kbPayload,
} from '@sd-jwt/types';
Expand Down
8 changes: 1 addition & 7 deletions packages/core/src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ describe('index', () => {
const presentation = await sdjwt.present(credential, ['foo'], {
kb: {
payload: {
sd_hash: 'sha-256',
aud: '1',
iat: 1,
nonce: '342',
Expand Down Expand Up @@ -154,8 +153,7 @@ describe('index', () => {
const presentation = await sdjwt.present(credential, ['foo'], {
kb: {
payload: {
sd_hash: '',
aud: '1',
aud: '',
iat: 1,
nonce: '342',
},
Expand Down Expand Up @@ -194,7 +192,6 @@ describe('index', () => {
const presentation = await sdjwt.present(credential, ['foo'], {
kb: {
payload: {
sd_hash: 'sha-256',
aud: '1',
iat: 1,
nonce: '342',
Expand Down Expand Up @@ -289,7 +286,6 @@ describe('index', () => {
const presentation = await sdjwt.present(credential, ['foo'], {
kb: {
payload: {
sd_hash: 'sha-256',
aud: '1',
iat: 1,
nonce: '342',
Expand Down Expand Up @@ -327,7 +323,6 @@ describe('index', () => {
const presentation = await sdjwt.present(credential, ['foo'], {
kb: {
payload: {
sd_hash: 'sha-256',
aud: '1',
iat: 1,
nonce: '342',
Expand Down Expand Up @@ -363,7 +358,6 @@ describe('index', () => {
const presentation = await sdjwt.present(credential, ['foo'], {
kb: {
payload: {
sd_hash: 'sha-256',
aud: '1',
iat: 1,
nonce: '342',
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type kbPayload = {
};

export type KBOptions = {
payload: kbPayload;
payload: Omit<kbPayload, 'sd_hash'>;
};

export type OrPromise<T> = T | Promise<T>;
Expand Down