Skip to content

Commit

Permalink
Merge pull request #77 from PeculiarVentures/microshine/issue57
Browse files Browse the repository at this point in the history
Issue 57
  • Loading branch information
microshine authored Jan 9, 2023
2 parents 71e66d3 + 154ef4e commit 576cb34
Show file tree
Hide file tree
Showing 9 changed files with 1,053 additions and 1,067 deletions.
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,37 @@
},
"homepage": "https://github.com/PeculiarVentures/xmldsigjs#readme",
"dependencies": {
"asn1js": "^2.4.0",
"pkijs": "^2.3.0",
"asn1js": "^3.0.5",
"pkijs": "^3.0.10",
"pvtsutils": "^1.3.2",
"pvutils": "^1.1.3",
"tslib": "^2.4.0",
"xml-core": "^1.1.4",
"tslib": "^2.4.1",
"xml-core": "^1.1.5",
"xpath": "^0.0.32"
},
"devDependencies": {
"@babel/core": "^7.17.10",
"@babel/plugin-proposal-class-properties": "^7.16.7",
"@babel/plugin-proposal-object-rest-spread": "^7.16.7",
"@babel/preset-env": "^7.17.10",
"@peculiar/webcrypto": "^1.2.3",
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-typescript": "^8.3.0",
"@types/asn1js": "^2.0.2",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.31",
"@xmldom/xmldom": "^0.8.2",
"@babel/core": "^7.20.12",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/preset-env": "^7.20.2",
"@peculiar/webcrypto": "^1.4.1",
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^11.0.0",
"@types/asn1js": "^3.0.7",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@xmldom/xmldom": "^0.8.6",
"coveralls": "^3.1.1",
"mocha": "^10.0.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"rollup": "^2.72.0",
"rollup": "^3.9.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-terser": "^7.0.2",
"ts-node": "^10.4.0",
"ts-node": "^10.9.1",
"tslint": "^6.1.3",
"typescript": "^4.6.4"
"typescript": "^4.9.4"
},
"nyc": {
"extension": [
Expand Down Expand Up @@ -104,4 +104,4 @@
"test/**/*.ts"
]
}
}
}
3 changes: 1 addition & 2 deletions rollup.config.js → rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import babel from "rollup-plugin-babel";
import { terser } from "rollup-plugin-terser";

const pkg = require("./package.json");
import pkg from "./package.json" assert { type: "json" };

const banner = [].join("\n");
const input = "src/index.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Application {
*/
public static setEngine(name: string, crypto: Crypto): void {
engineCrypto = Object.assign(crypto, {name});
setEngine(name, crypto, new CryptoEngine({ name, crypto, subtle: crypto.subtle }));
setEngine(name, new CryptoEngine({ name, crypto }));
}

/**
Expand Down
18 changes: 11 additions & 7 deletions src/pki/x509.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Asn1Js from "asn1js";
import { Certificate } from "pkijs";
import { Certificate, CryptoEngineAlgorithmParams } from "pkijs";
import { ECDSA } from "../algorithms";
import { Application } from "../application";

Expand Down Expand Up @@ -79,7 +79,7 @@ const OID: { [key: string]: { short?: string, long?: string; }; } = {
export class X509Certificate {

protected raw: Uint8Array;
protected simpl: any;
protected simpl: Certificate;
protected publicKey: CryptoKey | null = null;

constructor(rawData?: BufferSource) {
Expand Down Expand Up @@ -146,24 +146,29 @@ export class X509Certificate {
usages: ["verify"],
};
if (alg.algorithm.name.toUpperCase() === ECDSA) {
// Set named curve
(alg.algorithm as any).namedCurve = this.simpl.subjectPublicKeyInfo.toJSON().crv;
const json = this.simpl.subjectPublicKeyInfo.toJSON();
if ("crv" in json && json.crv) {
// Set named curve
(alg.algorithm as any).namedCurve = json.crv;
} else {
throw new Error("Cannot get Curved name from the ECDSA public key");
}
}
if (this.isHashedAlgorithm(alg.algorithm)) {
if (typeof alg.algorithm.hash === "string") {
alg.algorithm.hash = { name: alg.algorithm.hash };
}
}

const key = await this.simpl.getPublicKey({ algorithm: alg });
const key = await this.simpl.getPublicKey({ algorithm: alg as CryptoEngineAlgorithmParams });
this.publicKey = key;

return key;
}

if (this.simpl.subjectPublicKeyInfo.algorithm.algorithmId === "1.2.840.113549.1.1.1") {
// Use default hash algorithm for RSA keys. Otherwise it throws an exception for unsupported mechanism (eg md5WithRSAEncryption)
this.publicKey = await this.simpl.getPublicKey({ algorithm: { algorithm: { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } } }, usages: ["verify"] });
this.publicKey = await this.simpl.getPublicKey({ algorithm: { algorithm: { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } }, usages: ["verify"] } });
} else {
this.publicKey = await this.simpl.getPublicKey();
}
Expand Down Expand Up @@ -203,7 +208,6 @@ export class X509Certificate {
//#endregion

private isHashedAlgorithm(alg: Algorithm): alg is RsaHashedImportParams {
// @ts-ignore
return !!(alg)["hash"];
}
}
2 changes: 0 additions & 2 deletions src/signed_xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ export class SignedXml implements XmlCore.IXmlSerializable {
let signedInfo: SignedInfo;
const signingAlg = XmlCore.assign({}, algorithm);

// @ts-ignore
if (key.algorithm["hash"]) {
// @ts-ignore
signingAlg.hash = key.algorithm["hash"];
}
alg = CryptoConfig.GetSignatureAlgorithm(signingAlg);
Expand Down
2 changes: 1 addition & 1 deletion src/xml/key_infos/key_info_clause.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {XmlSignatureObject} from "../xml_object";
import { XmlSignatureObject } from "../xml_object";

export abstract class KeyInfoClause extends XmlSignatureObject {
public Key: CryptoKey | null;
Expand Down
15 changes: 3 additions & 12 deletions src/xml/key_infos/x509_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export class KeyInfoX509Data extends KeyInfoClause {
private SubjectKeyIdList: Uint8Array[] = [];
private SubjectNameList: string[];
private X509CertificateList: X509Certificate[];
private key: CryptoKey | null = null;

public constructor();
public constructor(rgbCert: Uint8Array);
Expand All @@ -105,14 +104,6 @@ export class KeyInfoX509Data extends KeyInfoClause {
}
}

/**
* Gets public key of the X509Data
*/
// @ts-ignore
public get Key(): CryptoKey | null {
return this.key;
}

public async importKey(key: CryptoKey): Promise<this> {
throw new XmlError(XE.METHOD_NOT_SUPPORTED);
}
Expand All @@ -126,9 +117,9 @@ export class KeyInfoX509Data extends KeyInfoClause {
if (!this.Certificates.length) {
throw new XmlError(XE.NULL_REFERENCE);
}
const key = await this.Certificates[0].exportKey(alg);
this.key = key;
return key;
this.Key = await this.Certificates[0].exportKey(alg);

return this.Key;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "ES2018",
"target": "ES2019",
"lib": [
"dom",
"es2015"
Expand All @@ -25,4 +25,4 @@
"exclude": [
"build"
]
}
}
Loading

0 comments on commit 576cb34

Please sign in to comment.