Skip to content

Commit

Permalink
fix: Incompatible with TypeScript 4.4 #89
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Dec 7, 2021
1 parent 0f78387 commit 7f5f7cf
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/mechs/ec/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class EcCrypto {
}
}

public static async generateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public static async generateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
this.checkLib();

const key = this.initEcKey(algorithm.namedCurve);
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/ec/ec_dh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EcCryptoKey } from "./key";

export class EcdhProvider extends core.EcdhProvider {

public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
return EcCrypto.generateKey(algorithm, extractable, keyUsages);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mechs/ec/ec_dsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function buffer2hex(buffer: Uint8Array, padded?: boolean): string {

export class EcdsaProvider extends core.EcdsaProvider {

public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
return EcCrypto.generateKey(algorithm, extractable, keyUsages);
}

Expand Down
29 changes: 23 additions & 6 deletions src/mechs/ed/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export class EdCrypto {
return res;
}

public static async generateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public static async generateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
this.checkLib();

const curve = algorithm.namedCurve.toLowerCase() === "x25519" ? "curve25519" : "ed25519"; // "x25519" | "ed25519"
// const curve = algorithm.namedCurve.toLowerCase() === "x25519" ? "curve25519" : "ed25519"; // "x25519" | "ed25519"
const curve = "ed25519";
let edKey: EllipticJS.EllipticKeyPair;
if (curve === "ed25519") {
const raw = nativeCrypto.getRandomValues(new Uint8Array(32));
Expand Down Expand Up @@ -86,7 +87,22 @@ export class EdCrypto {
public static async deriveBits(algorithm: EcdhKeyDeriveParams, baseKey: EdPrivateKey, length: number): Promise<ArrayBuffer> {
this.checkLib();

const shared = baseKey.data.derive((algorithm.public as EdPublicKey).data.getPublic());
const key = new Uint8Array(Convert.FromHex(baseKey.data.getSecret("hex")));
// key[0] &= 248;
// key[31] &= 127;
// key[31] |= 64;
// key.reverse();

// @ts-ignore
const ecdh = new elliptic.ec("curve25519");
const privateKey = ecdh.keyFromPrivate(Convert.ToHex(key), "hex");

const publicHex = (algorithm.public as EdPublicKey).data.getPublic("hex") as string;
const publicView = new Uint8Array(Convert.FromHex(publicHex));
// publicView.reverse();
// const publicKey = ecdh.keyFromPublic(Convert.ToHex(publicView), "hex").getPublic();
const publicKey = (algorithm.public as EdPublicKey).data.getPublic();
const shared = privateKey.derive(publicKey);
let array = new Uint8Array(shared.toArray());

// Padding
Expand All @@ -106,9 +122,10 @@ export class EdCrypto {
case "jwk":
return JsonSerializer.toJSON(key);
case "pkcs8": {
const raw = Convert.FromHex(/^x/i.test(key.algorithm.namedCurve)
? key.data.getPrivate("hex")
: key.data.getSecret("hex"));
// const raw = Convert.FromHex(/^x/i.test(key.algorithm.namedCurve)
// ? key.data.getPrivate("hex")
// : key.data.getSecret("hex"));
const raw = Convert.FromHex(key.data.getSecret("hex"));
const keyInfo = new core.asn1.PrivateKeyInfo();
keyInfo.privateKeyAlgorithm.algorithm = getOidByNamedCurve(key.algorithm.namedCurve);
keyInfo.privateKey = AsnConvert.serialize(new OctetString(raw));
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/ed/ecdh_es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class EcdhEsProvider extends core.EcdhEsProvider {

public namedCurves: string[] = ["X25519"];

public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
const keys = await EdCrypto.generateKey(
{
name: this.name,
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/ed/eddsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class EdDsaProvider extends core.EdDsaProvider {

public namedCurves: string[] = ["Ed25519"];

public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
const keys = await EdCrypto.generateKey(
{
name: this.name,
Expand Down
5 changes: 3 additions & 2 deletions src/mechs/ed/private_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export class EdPrivateKey extends CryptoKey implements IJsonConvertible {
}

const hexPrivateKey = Convert.ToHex(Convert.FromBase64Url(json.d));
if (/^ed/i.test(json.crv)) {
const eddsa = new elliptic.eddsa(json.crv.toLowerCase());
if (true || /^ed/i.test(json.crv)) {
// const eddsa = new elliptic.eddsa(json.crv.toLowerCase());
const eddsa = new elliptic.eddsa("ed25519");
this.data = eddsa.keyFromSecret(hexPrivateKey);
} else {
const ecdhEs = elliptic.ec(json.crv.replace(/^x/i, "curve"));
Expand Down
4 changes: 2 additions & 2 deletions src/mechs/rsa/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export class RsaCrypto {
}
}

public static async generateKey(algorithm: RsaHashedKeyGenParams | RsaKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public static async generateKey(algorithm: RsaHashedKeyGenParams | RsaKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
const alg: RsaHashedKeyGenParams = {
name: "RSA-PSS",
hash: "SHA-256",
publicExponent: algorithm.publicExponent,
modulusLength: algorithm.modulusLength,
};
// generate keys using native crypto
const keys = (await nativeSubtle.generateKey(alg, true, ["sign", "verify"])) as CryptoKeyPair;
const keys = (await nativeSubtle.generateKey(alg, true, ["sign", "verify"])) as core.CryptoKeyPair;
const crypto = new Crypto();

// create private key
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/rsa/rsa_es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class RsaEsProvider extends core.ProviderCrypto {
};
public hashAlgorithms = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"];

public async onGenerateKey(algorithm: RsaKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: RsaKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
return RsaCrypto.generateKey(algorithm, extractable, keyUsages);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mechs/rsa/rsa_oaep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RsaCryptoKey } from "./key";

export class RsaOaepProvider extends core.RsaOaepProvider {

public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
return RsaCrypto.generateKey(algorithm, extractable, keyUsages);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mechs/rsa/rsa_pss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RsaCryptoKey } from "./key";

export class RsaPssProvider extends core.RsaPssProvider {

public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
return RsaCrypto.generateKey(algorithm, extractable, keyUsages);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mechs/rsa/rsa_ssa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RsaCryptoKey } from "./key";

export class RsaSsaProvider extends core.RsaSsaProvider {

public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
return RsaCrypto.generateKey(algorithm, extractable, keyUsages);
}

Expand Down
9 changes: 7 additions & 2 deletions src/subtle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class SubtleCrypto extends core.SubtleCrypto {

//#region ECDH-ES
// TODO Elliptic.js has got issue (https://github.com/indutny/elliptic/issues/243). Uncomment the next line after fix
// this.providers.set(new EcdhEsProvider());
this.providers.set(new EcdhEsProvider());
//#endregion

}
Expand Down Expand Up @@ -220,7 +220,12 @@ export class SubtleCrypto extends core.SubtleCrypto {
}
}

return super[method].apply(this, args);
const fn = super[method];
if (typeof fn === "function") {
return fn.apply(this, args);
}

throw new Error("Incorrect type of 'method'. Must be 'function'.");
}

private fixNativeArguments(method: SubtleMethods, args: any[]) {
Expand Down
2 changes: 0 additions & 2 deletions test/ed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,4 @@ context("ED", () => {

});



});

0 comments on commit 7f5f7cf

Please sign in to comment.