Skip to content

Commit

Permalink
Some improvement due to deno
Browse files Browse the repository at this point in the history
  • Loading branch information
iherman committed Mar 8, 2024
1 parent e9d34c2 commit bd64275
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
5 changes: 3 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import { isKeyData, isDatasetCore, convertToStore, DatasetMap, GraphWithID, calc
import { generateAProofGraph, verifyProofGraphs, rdf_type, sec_di_proof, sec_proof, sec_prefix } from './lib/proof_utils';

/* This file is also the "top level", so a number of exports are put here to be more friendly to users */
export { KeyData, VerificationResult, KeyMetadata, Cryptosuites } from './lib/types';
export { generateKey, KeyDetails } from './lib/crypto_utils';
export type { KeyData, VerificationResult, KeyMetadata, Cryptosuites } from './lib/types';
export type { KeyDetails } from './lib/crypto_utils';
export { generateKey } from './lib/crypto_utils';

// n3.DataFactory is a namespace with some functions...
const { quad } = n3.DataFactory;
Expand Down
32 changes: 18 additions & 14 deletions lib/crypto_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ const RsaAlgs: Record<Alg, WebCryptoAPIData> = {
*/
function algorithmData(report: Errors, key: JsonWebKey): WebCryptoAPIData | null {
switch (key.kty as Kty) {
case "EC" : {
return {
name: "ECDSA",
namedCurve: key.crv as Crv,
hash: DEFAULT_HASH
}
}
case "RSA" : {
try {
return RsaAlgs[key.alg as Alg];
Expand All @@ -90,6 +83,13 @@ function algorithmData(report: Errors, key: JsonWebKey): WebCryptoAPIData | null
return null;
}
}
case "EC": default: {
return {
name: "ECDSA",
namedCurve: key.crv as Crv,
hash: DEFAULT_HASH
};
}
}
}

Expand Down Expand Up @@ -264,13 +264,17 @@ export function cryptosuiteId(report: Errors, keyPair: KeyPair): Cryptosuites |
}

const alg = algorithmData(report, keyPair.public);
switch (alg.name) {
case "ECDSA": return Cryptosuites.ecdsa;
case "RSA-PSS": return Cryptosuites.rsa_pss;
case "RSASSA-PKCS1-v1_5": return Cryptosuites.rsa_ssa;
default: {
report.errors.push(new types.Invalid_Verification_Method(`Unknown alg (${alg.name} in:\n ${JSON.stringify(keyPair,null,4)})`));
return null;
if (alg === null) {
return null;
} else {
switch (alg.name) {
case "ECDSA": return Cryptosuites.ecdsa;
case "RSA-PSS": return Cryptosuites.rsa_pss;
case "RSASSA-PKCS1-v1_5": return Cryptosuites.rsa_ssa;
default: {
report.errors.push(new types.Invalid_Verification_Method(`Unknown alg (${alg.name} in:\n ${JSON.stringify(keyPair,null,4)})`));
return null;
}
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions lib/proof_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const xsd_datetime: rdf.NamedNode = xsd_prefix('dateTime');
* @param keyData
* @returns
*/
export async function generateAProofGraph(report: Errors, hashValue: string, keyData: KeyData): Promise < rdf.DatasetCore > {
export async function generateAProofGraph(report: Errors, hashValue: string, keyData: KeyData): Promise <rdf.DatasetCore> {
const cryptosuite = keyData?.cryptosuite || cryptosuiteId(report, keyData)

// Create a proof graph. Just a boring set of quad generations...
Expand Down Expand Up @@ -104,7 +104,15 @@ export async function generateAProofGraph(report: Errors, hashValue: string, key
if (keyData.revoked) retval.add(quad(keyResource, sec_revoked, literal(keyData.revoked, xsd_datetime)));
return retval;
};
return createProofGraph(await sign(report, hashValue, keyData.private));

const signature = await sign(report, hashValue, keyData.private);
if (signature === null) {
// An error has occurred during signature; details are in the report.
// No proof graph is generated
return new n3.Store();
} else {
return createProofGraph(signature);
}
};

/**
Expand Down Expand Up @@ -262,6 +270,7 @@ async function verifyAProofGraph(report: Errors, hash: string, proof: n3.Store,
*/
export async function verifyProofGraphs(report: Errors, hash: string, proofs: GraphWithID[]): Promise<boolean> {
const allErrors: Errors[] = [];
// deno-lint-ignore require-await
const singleVerification = async (pr: GraphWithID): Promise<boolean> => {
const singleReport: Errors = { errors: [], warnings: [] }
allErrors.push(singleReport);
Expand Down
5 changes: 4 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export class DatasetMap {
* @param obj
* @returns
*/
// deno-lint-ignore no-explicit-any
export function isDatasetCore(obj: any): obj is rdf.DatasetCore {
return (obj as rdf.DatasetCore).add !== undefined &&
(obj as rdf.DatasetCore).delete !== undefined &&
Expand All @@ -140,6 +141,7 @@ export function isDatasetCore(obj: any): obj is rdf.DatasetCore {
* @param obj
* @returns
*/
// deno-lint-ignore no-explicit-any
export function isKeyData(obj: any): obj is KeyMetadata {
return (obj as KeyPair).public !== undefined && (obj as KeyPair).private !== undefined;
}
Expand Down Expand Up @@ -202,5 +204,6 @@ const prefixes = {
export function write_quads(dataset: rdf.DatasetCore) {
const writer = new n3.Writer({ prefixes: prefixes });
for (const q of dataset) writer.addQuad(q);
writer.end((error, result) => console.log(result));
// deno-lint-ignore no-explicit-any
writer.end((_error: any, result: any) => console.log(result));
}
5 changes: 4 additions & 1 deletion misc/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
"@tufjs/canonical-json": "npm:@tufjs/canonical-json",
"n3": "npm:n3",
"uuid": "npm:uuid",
"./lib/errors": "./lib/errors.ts",
"./lib/utils": "./lib/utils.ts",
"../../index": "../../index.ts",
"./types": "./types.ts",
"./lib/types": "./lib/types.ts",
"./lib/proof_utils": "./lib/proof_utils.ts",
"./lib/crypto_utils": "./lib/crypto_utils.ts",
"./keys": "./keys.ts",
"./rdfn3": "./rdfn3.ts"
}
Expand Down
5 changes: 4 additions & 1 deletion testing/run/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import * as process from 'node:process';

import { KeyData, VerificationResult,
generateProofGraph, verifyProofGraph,
embedProofGraph, verifyEmbeddedProofGraph } from '../../index';
embedProofGraph, verifyEmbeddedProofGraph,
generateKey
} from '../../index';
import { get_quads, DataFactory, write_quads } from './rdfn3';
import { get_keys, OSet } from './keys';
import { Cryptosuites } from '../../lib/types';

function displayVerificationResult(result: VerificationResult): void {
console.log(`>>>> Verification result`);
Expand Down

0 comments on commit bd64275

Please sign in to comment.