diff --git a/packages/core/src/aes/base.ts b/packages/core/src/aes/base.ts index 76f11fc..f224394 100644 --- a/packages/core/src/aes/base.ts +++ b/packages/core/src/aes/base.ts @@ -1,6 +1,6 @@ import * as types from "@peculiar/webcrypto-types"; import { ProviderCrypto } from "../provider"; -import { BaseCryptoKey } from "../crypto_key"; +import { CryptoKey } from "../crypto_key"; export abstract class AesProvider extends ProviderCrypto { @@ -24,8 +24,8 @@ export abstract class AesProvider extends ProviderCrypto { this.checkGenerateKeyParams(algorithm); } - public abstract override onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; - public abstract override onExportKey(format: types.KeyFormat, key: BaseCryptoKey, ...args: any[]): Promise; - public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onExportKey(format: types.KeyFormat, key: CryptoKey, ...args: any[]): Promise; + public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; } diff --git a/packages/core/src/crypto_key.ts b/packages/core/src/crypto_key.ts index 741d6e8..6808b54 100644 --- a/packages/core/src/crypto_key.ts +++ b/packages/core/src/crypto_key.ts @@ -2,9 +2,9 @@ import * as types from "@peculiar/webcrypto-types"; const KEY_TYPES = ["secret", "private", "public"]; -export class BaseCryptoKey implements types.CryptoKey { +export class CryptoKey implements types.CryptoKey { - public static create(this: new () => T, algorithm: types.KeyAlgorithm, type: types.KeyType, extractable: boolean, usages: types.KeyUsages): T { + public static create(this: new () => T, algorithm: types.KeyAlgorithm, type: types.KeyType, extractable: boolean, usages: types.KeyUsages): T { const key = new this(); key.algorithm = algorithm; key.type = type; @@ -18,10 +18,10 @@ export class BaseCryptoKey implements types.CryptoKey { return KEY_TYPES.indexOf(data) !== -1; } - public algorithm!: types.KeyAlgorithm; - public type!: types.KeyType; - public usages!: types.KeyUsages; - public extractable!: boolean; + public algorithm: types.KeyAlgorithm = { name: "" }; + public type: types.KeyType = "secret"; + public usages: types.KeyUsages = []; + public extractable: boolean = false; // @internal public get [Symbol.toStringTag]() { diff --git a/packages/core/src/des/base.ts b/packages/core/src/des/base.ts index 5844265..17b3e78 100644 --- a/packages/core/src/des/base.ts +++ b/packages/core/src/des/base.ts @@ -1,6 +1,6 @@ import * as types from "@peculiar/webcrypto-types"; import { OperationError } from "../errors"; -import { BaseCryptoKey } from "../crypto_key"; +import { CryptoKey } from "../crypto_key"; import { ProviderCrypto } from "../provider"; import { BufferSource } from "pvtsutils"; @@ -56,10 +56,10 @@ export abstract class DesProvider extends ProviderCrypto { this.checkGenerateKeyParams(algorithm); } - public abstract override onGenerateKey(algorithm: DesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; - public abstract override onExportKey(format: types.KeyFormat, key: BaseCryptoKey, ...args: any[]): Promise; - public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: DesImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; - public abstract override onEncrypt(algorithm: DesParams, key: BaseCryptoKey, data: ArrayBuffer, ...args: any[]): Promise; - public abstract override onDecrypt(algorithm: DesParams, key: BaseCryptoKey, data: ArrayBuffer, ...args: any[]): Promise; + public abstract override onGenerateKey(algorithm: DesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onExportKey(format: types.KeyFormat, key: CryptoKey, ...args: any[]): Promise; + public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: DesImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onEncrypt(algorithm: DesParams, key: CryptoKey, data: ArrayBuffer, ...args: any[]): Promise; + public abstract override onDecrypt(algorithm: DesParams, key: CryptoKey, data: ArrayBuffer, ...args: any[]): Promise; } diff --git a/packages/core/src/ec/base.ts b/packages/core/src/ec/base.ts index bb50464..7bbdecf 100644 --- a/packages/core/src/ec/base.ts +++ b/packages/core/src/ec/base.ts @@ -1,6 +1,6 @@ import * as types from "@peculiar/webcrypto-types"; import { OperationError } from "../errors"; -import { BaseCryptoKey } from "../crypto_key"; +import { CryptoKey } from "../crypto_key"; import { ProviderCrypto } from "../provider"; export abstract class EllipticProvider extends ProviderCrypto { @@ -23,7 +23,7 @@ export abstract class EllipticProvider extends ProviderCrypto { } public abstract override onGenerateKey(algorithm: types.EcKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; - public abstract override onExportKey(format: types.KeyFormat, key: BaseCryptoKey, ...args: any[]): Promise; - public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onExportKey(format: types.KeyFormat, key: CryptoKey, ...args: any[]): Promise; + public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; } diff --git a/packages/core/src/ec/curves.ts b/packages/core/src/ec/curves.ts index c4197cc..6b42801 100644 --- a/packages/core/src/ec/curves.ts +++ b/packages/core/src/ec/curves.ts @@ -1,5 +1,5 @@ import { AsnConvert } from "@peculiar/asn1-schema"; -import * as schema from "packages/core/src/schema"; +import * as asn1 from "../schema/asn1"; export interface EcCurveParams { /** @@ -28,7 +28,7 @@ export class EcCurves { private constructor() { } public static register(item: EcCurveParams) { - const oid = new schema.ObjectIdentifier(); + const oid = new asn1.ObjectIdentifier(); oid.value = item.id; const raw = AsnConvert.serialize(oid); @@ -61,21 +61,21 @@ export class EcCurves { } -EcCurves.register({ name: "P-256", id: schema.idSecp256r1, size: 256 }); -EcCurves.register({ name: "P-384", id: schema.idSecp384r1, size: 384 }); -EcCurves.register({ name: "P-521", id: schema.idSecp521r1, size: 521 }); -EcCurves.register({ name: "K-256", id: schema.idSecp256k1, size: 256 }); -EcCurves.register({ name: "brainpoolP160r1", id: schema.idBrainpoolP160r1, size: 160 }); -EcCurves.register({ name: "brainpoolP160t1", id: schema.idBrainpoolP160t1, size: 160 }); -EcCurves.register({ name: "brainpoolP192r1", id: schema.idBrainpoolP192r1, size: 192 }); -EcCurves.register({ name: "brainpoolP192t1", id: schema.idBrainpoolP192t1, size: 192 }); -EcCurves.register({ name: "brainpoolP224r1", id: schema.idBrainpoolP224r1, size: 224 }); -EcCurves.register({ name: "brainpoolP224t1", id: schema.idBrainpoolP224t1, size: 224 }); -EcCurves.register({ name: "brainpoolP256r1", id: schema.idBrainpoolP256r1, size: 256 }); -EcCurves.register({ name: "brainpoolP256t1", id: schema.idBrainpoolP256t1, size: 256 }); -EcCurves.register({ name: "brainpoolP320r1", id: schema.idBrainpoolP320r1, size: 320 }); -EcCurves.register({ name: "brainpoolP320t1", id: schema.idBrainpoolP320t1, size: 320 }); -EcCurves.register({ name: "brainpoolP384r1", id: schema.idBrainpoolP384r1, size: 384 }); -EcCurves.register({ name: "brainpoolP384t1", id: schema.idBrainpoolP384t1, size: 384 }); -EcCurves.register({ name: "brainpoolP512r1", id: schema.idBrainpoolP512r1, size: 512 }); -EcCurves.register({ name: "brainpoolP512t1", id: schema.idBrainpoolP512t1, size: 512 }); +EcCurves.register({ name: "P-256", id: asn1.idSecp256r1, size: 256 }); +EcCurves.register({ name: "P-384", id: asn1.idSecp384r1, size: 384 }); +EcCurves.register({ name: "P-521", id: asn1.idSecp521r1, size: 521 }); +EcCurves.register({ name: "K-256", id: asn1.idSecp256k1, size: 256 }); +EcCurves.register({ name: "brainpoolP160r1", id: asn1.idBrainpoolP160r1, size: 160 }); +EcCurves.register({ name: "brainpoolP160t1", id: asn1.idBrainpoolP160t1, size: 160 }); +EcCurves.register({ name: "brainpoolP192r1", id: asn1.idBrainpoolP192r1, size: 192 }); +EcCurves.register({ name: "brainpoolP192t1", id: asn1.idBrainpoolP192t1, size: 192 }); +EcCurves.register({ name: "brainpoolP224r1", id: asn1.idBrainpoolP224r1, size: 224 }); +EcCurves.register({ name: "brainpoolP224t1", id: asn1.idBrainpoolP224t1, size: 224 }); +EcCurves.register({ name: "brainpoolP256r1", id: asn1.idBrainpoolP256r1, size: 256 }); +EcCurves.register({ name: "brainpoolP256t1", id: asn1.idBrainpoolP256t1, size: 256 }); +EcCurves.register({ name: "brainpoolP320r1", id: asn1.idBrainpoolP320r1, size: 320 }); +EcCurves.register({ name: "brainpoolP320t1", id: asn1.idBrainpoolP320t1, size: 320 }); +EcCurves.register({ name: "brainpoolP384r1", id: asn1.idBrainpoolP384r1, size: 384 }); +EcCurves.register({ name: "brainpoolP384t1", id: asn1.idBrainpoolP384t1, size: 384 }); +EcCurves.register({ name: "brainpoolP512r1", id: asn1.idBrainpoolP512r1, size: 512 }); +EcCurves.register({ name: "brainpoolP512t1", id: asn1.idBrainpoolP512t1, size: 512 }); diff --git a/packages/core/src/ec/ecdh.ts b/packages/core/src/ec/ecdh.ts index ea57478..c7ea89e 100644 --- a/packages/core/src/ec/ecdh.ts +++ b/packages/core/src/ec/ecdh.ts @@ -1,6 +1,6 @@ import * as types from "@peculiar/webcrypto-types"; import { OperationError } from "../errors"; -import { BaseCryptoKey } from "../crypto_key"; +import { CryptoKey } from "../crypto_key"; import { EllipticProvider } from "./base"; export abstract class EcdhProvider extends EllipticProvider { @@ -17,7 +17,7 @@ export abstract class EcdhProvider extends EllipticProvider { public override checkAlgorithmParams(algorithm: types.EcdhKeyDeriveParams) { // public this.checkRequiredProperty(algorithm, "public"); - if (!(algorithm.public instanceof BaseCryptoKey)) { + if (!(algorithm.public instanceof CryptoKey)) { throw new TypeError("public: Is not a CryptoKey"); } if (algorithm.public.type !== "public") { @@ -28,6 +28,6 @@ export abstract class EcdhProvider extends EllipticProvider { } } - public abstract override onDeriveBits(algorithm: types.EcdhKeyDeriveParams, baseKey: BaseCryptoKey, length: number, ...args: any[]): Promise; + public abstract override onDeriveBits(algorithm: types.EcdhKeyDeriveParams, baseKey: CryptoKey, length: number, ...args: any[]): Promise; } diff --git a/packages/core/src/hkdf/hkdf.ts b/packages/core/src/hkdf/hkdf.ts index dcd7d20..0cc617b 100644 --- a/packages/core/src/hkdf/hkdf.ts +++ b/packages/core/src/hkdf/hkdf.ts @@ -1,6 +1,6 @@ import * as types from "@peculiar/webcrypto-types"; import { BufferSourceConverter } from "pvtsutils"; -import { BaseCryptoKey } from "../crypto_key"; +import { CryptoKey } from "../crypto_key"; import { ProviderCrypto } from "../provider"; export abstract class HkdfProvider extends ProviderCrypto { @@ -35,7 +35,7 @@ export abstract class HkdfProvider extends ProviderCrypto { } } - public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; - public abstract override onDeriveBits(algorithm: types.HkdfParams, baseKey: BaseCryptoKey, length: number, ...args: any[]): Promise; + public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onDeriveBits(algorithm: types.HkdfParams, baseKey: CryptoKey, length: number, ...args: any[]): Promise; } diff --git a/packages/core/src/hmac/index.ts b/packages/core/src/hmac/index.ts index 4e18895..2c06977 100644 --- a/packages/core/src/hmac/index.ts +++ b/packages/core/src/hmac/index.ts @@ -1,5 +1,5 @@ import * as types from "@peculiar/webcrypto-types"; -import { BaseCryptoKey } from "../crypto_key"; +import { CryptoKey } from "../crypto_key"; import { ProviderCrypto } from "../provider"; export abstract class HmacProvider extends ProviderCrypto { @@ -49,8 +49,8 @@ export abstract class HmacProvider extends ProviderCrypto { this.checkHashAlgorithm(algorithm.hash as types.Algorithm, this.hashAlgorithms); } - public abstract override onGenerateKey(algorithm: types.PreparedHashedAlgorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; - public abstract override onExportKey(format: types.KeyFormat, key: BaseCryptoKey, ...args: any[]): Promise; - public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.HmacImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onGenerateKey(algorithm: types.PreparedHashedAlgorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onExportKey(format: types.KeyFormat, key: CryptoKey, ...args: any[]): Promise; + public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.HmacImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; } diff --git a/packages/core/src/pbkdf/pbkdf2.ts b/packages/core/src/pbkdf/pbkdf2.ts index 7945025..76eaa36 100644 --- a/packages/core/src/pbkdf/pbkdf2.ts +++ b/packages/core/src/pbkdf/pbkdf2.ts @@ -1,5 +1,5 @@ import * as types from "@peculiar/webcrypto-types"; -import { BaseCryptoKey } from "../crypto_key"; +import { CryptoKey } from "../crypto_key"; import { ProviderCrypto } from "../provider"; export abstract class Pbkdf2Provider extends ProviderCrypto { @@ -39,7 +39,7 @@ export abstract class Pbkdf2Provider extends ProviderCrypto { } } - public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; - public abstract override onDeriveBits(algorithm: types.Pbkdf2Params, baseKey: BaseCryptoKey, length: number, ...args: any[]): Promise; + public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onDeriveBits(algorithm: types.Pbkdf2Params, baseKey: CryptoKey, length: number, ...args: any[]): Promise; } diff --git a/packages/core/src/rsa/base.ts b/packages/core/src/rsa/base.ts index 1ff71e4..d6ed546 100644 --- a/packages/core/src/rsa/base.ts +++ b/packages/core/src/rsa/base.ts @@ -1,6 +1,6 @@ import * as types from "@peculiar/webcrypto-types"; import { Convert } from "pvtsutils"; -import { BaseCryptoKey } from "../crypto_key"; +import { CryptoKey } from "../crypto_key"; import { ProviderCrypto } from "../provider"; export abstract class RsaProvider extends ProviderCrypto { @@ -37,7 +37,7 @@ export abstract class RsaProvider extends ProviderCrypto { } public abstract override onGenerateKey(algorithm: types.RsaHashedKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; - public abstract override onExportKey(format: types.KeyFormat, key: BaseCryptoKey, ...args: any[]): Promise; - public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; + public abstract override onExportKey(format: types.KeyFormat, key: CryptoKey, ...args: any[]): Promise; + public abstract override onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise; } diff --git a/packages/core/src/schema/asn1/converters/index.ts b/packages/core/src/schema/asn1/converters/index.ts index c9c6ef5..d8ea989 100644 --- a/packages/core/src/schema/asn1/converters/index.ts +++ b/packages/core/src/schema/asn1/converters/index.ts @@ -1 +1,2 @@ export * from "./integer_without_paddings"; +export * from "./integer_converter"; diff --git a/packages/core/src/schema/json/converters/integer_converter.ts b/packages/core/src/schema/asn1/converters/integer_converter.ts similarity index 100% rename from packages/core/src/schema/json/converters/integer_converter.ts rename to packages/core/src/schema/asn1/converters/integer_converter.ts diff --git a/packages/core/src/schema/asn1/index.ts b/packages/core/src/schema/asn1/index.ts index 08cf577..3f95ce6 100644 --- a/packages/core/src/schema/asn1/index.ts +++ b/packages/core/src/schema/asn1/index.ts @@ -11,4 +11,4 @@ export * from "./one_asymmetric_key"; export * from "./ed_private_key"; export * from "./ed_public_key"; export * from "./rfc8410"; -export * as converters from "./converters"; +export * from "./converters"; diff --git a/packages/core/src/schema/asn1/rsa_private_key.ts b/packages/core/src/schema/asn1/rsa_private_key.ts index 108b954..8d81e8e 100644 --- a/packages/core/src/schema/asn1/rsa_private_key.ts +++ b/packages/core/src/schema/asn1/rsa_private_key.ts @@ -1,6 +1,7 @@ -import { AsnIntegerConverter, AsnProp, AsnPropTypes } from "@peculiar/asn1-schema"; +import { AsnIntegerConverter, AsnProp, AsnPropTypes, AsnType, AsnTypeTypes } from "@peculiar/asn1-schema"; import { JsonProp } from "@peculiar/json-schema"; -import { AsnIntegerArrayBufferConverter, JsonBase64UrlArrayBufferConverter } from "../json/converters"; +import { JsonBase64UrlArrayBufferConverter } from "../json/converters"; +import { AsnIntegerArrayBufferConverter } from "./converters"; // RFC 3437 // https://tools.ietf.org/html/rfc3447#appendix-A.1.2 @@ -18,6 +19,7 @@ import { AsnIntegerArrayBufferConverter, JsonBase64UrlArrayBufferConverter } fro // otherPrimeInfos OtherPrimeInfos OPTIONAL // } +@AsnType({ type: AsnTypeTypes.Sequence }) export class RsaPrivateKey { @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerConverter }) diff --git a/packages/core/src/schema/asn1/rsa_public_key.ts b/packages/core/src/schema/asn1/rsa_public_key.ts index 287abe0..46f1d46 100644 --- a/packages/core/src/schema/asn1/rsa_public_key.ts +++ b/packages/core/src/schema/asn1/rsa_public_key.ts @@ -1,6 +1,7 @@ import { AsnProp, AsnPropTypes } from "@peculiar/asn1-schema"; import { JsonProp } from "@peculiar/json-schema"; -import { JsonBase64UrlArrayBufferConverter, AsnIntegerArrayBufferConverter } from "../json/converters"; +import { JsonBase64UrlArrayBufferConverter } from "../json/converters"; +import { AsnIntegerArrayBufferConverter } from "./converters"; // RFC 3437 // https://tools.ietf.org/html/rfc3447#appendix-A.1.1 diff --git a/packages/core/src/schema/index.ts b/packages/core/src/schema/index.ts index 34f29ef..27b1f5a 100644 --- a/packages/core/src/schema/index.ts +++ b/packages/core/src/schema/index.ts @@ -1,2 +1,2 @@ -export * from "./asn1"; -export * from "./json"; \ No newline at end of file +export * as asn1 from "./asn1"; +export * as json from "./json"; \ No newline at end of file diff --git a/packages/core/src/schema/json/converters/index.ts b/packages/core/src/schema/json/converters/index.ts index da971ca..a90acc1 100644 --- a/packages/core/src/schema/json/converters/index.ts +++ b/packages/core/src/schema/json/converters/index.ts @@ -1,2 +1 @@ export * from "./base64_url"; -export * from "./integer_converter"; diff --git a/packages/core/src/subtle.ts b/packages/core/src/subtle.ts index 526396c..b558597 100644 --- a/packages/core/src/subtle.ts +++ b/packages/core/src/subtle.ts @@ -3,7 +3,7 @@ import { BufferSource, BufferSourceConverter, Convert } from "pvtsutils"; import { AlgorithmError } from "./errors"; import { ProviderCrypto } from "./provider"; import { ProviderStorage } from "./storage"; -import { BaseCryptoKey } from './crypto_key'; +import { CryptoKey } from './crypto_key'; export class SubtleCrypto implements types.SubtleCrypto { @@ -241,8 +241,8 @@ export class SubtleCrypto implements types.SubtleCrypto { return provider; } - protected checkCryptoKey(key: types.CryptoKey): asserts key is BaseCryptoKey { - if (!(key instanceof BaseCryptoKey)) { + protected checkCryptoKey(key: types.CryptoKey): asserts key is CryptoKey { + if (!(key instanceof CryptoKey)) { throw new TypeError(`Key is not of type 'CryptoKey'`); } } diff --git a/packages/core/test/des.spec.ts b/packages/core/test/des.spec.ts index 6506814..9695b18 100644 --- a/packages/core/test/des.spec.ts +++ b/packages/core/test/des.spec.ts @@ -7,19 +7,19 @@ class DesTestProvider extends core.DesProvider { public ivSize = 8; public name = "DES-TEST"; - public onGenerateKey(algorithm: import("../src/des").DesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public onGenerateKey(algorithm: import("../src/des").DesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { throw new Error("Method not implemented."); } - public onExportKey(format: types.KeyFormat, key: core.BaseCryptoKey): Promise { + public onExportKey(format: types.KeyFormat, key: core.CryptoKey): Promise { throw new Error("Method not implemented."); } - public onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: import("../src/des").DesImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: import("../src/des").DesImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { throw new Error("Method not implemented."); } - public onEncrypt(algorithm: import("../src/des").DesParams, key: core.BaseCryptoKey, data: ArrayBuffer): Promise { + public onEncrypt(algorithm: import("../src/des").DesParams, key: core.CryptoKey, data: ArrayBuffer): Promise { throw new Error("Method not implemented."); } - public onDecrypt(algorithm: import("../src/des").DesParams, key: core.BaseCryptoKey, data: ArrayBuffer): Promise { + public onDecrypt(algorithm: import("../src/des").DesParams, key: core.CryptoKey, data: ArrayBuffer): Promise { throw new Error("Method not implemented."); } } diff --git a/packages/core/test/ec.spec.ts b/packages/core/test/ec.spec.ts index 512d898..d1c05e4 100644 --- a/packages/core/test/ec.spec.ts +++ b/packages/core/test/ec.spec.ts @@ -54,10 +54,10 @@ context("EC", () => { public onGenerateKey(algorithm: types.EcKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { throw new Error("Method not implemented."); } - public onExportKey(format: types.KeyFormat, key: core.BaseCryptoKey): Promise { + public onExportKey(format: types.KeyFormat, key: core.CryptoKey): Promise { throw new Error("Method not implemented."); } - public onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { throw new Error("Method not implemented."); } } @@ -115,7 +115,7 @@ context("EC", () => { it("error if `public` is not public CryptoKey", () => { assert.throws(() => { - const key = new core.BaseCryptoKey(); + const key = new core.CryptoKey(); key.type = "secret"; provider.checkAlgorithmParams({ public: key } as any); }, Error); @@ -123,7 +123,7 @@ context("EC", () => { it("error if `public` is wrong CryptoKey alg", () => { assert.throws(() => { - const key = new core.BaseCryptoKey(); + const key = new core.CryptoKey(); key.type = "public"; key.algorithm = { name: "ECDSA" }; provider.checkAlgorithmParams({ public: key } as any); @@ -131,7 +131,7 @@ context("EC", () => { }); it("correct `public`", () => { - const key = new core.BaseCryptoKey(); + const key = new core.CryptoKey(); key.type = "public"; key.algorithm = { name: "ECDH" }; provider.checkAlgorithmParams({ public: key } as any); @@ -171,16 +171,16 @@ context("EC", () => { context("ECDH-ES", () => { class TestEcdhEsProvider extends core.EcdhEsProvider { - public async onDeriveBits(algorithm: types.EcdhKeyDeriveParams, baseKey: core.BaseCryptoKey, length: number, ...args: any[]): Promise { + public async onDeriveBits(algorithm: types.EcdhKeyDeriveParams, baseKey: core.CryptoKey, length: number, ...args: any[]): Promise { return null as any; } public async onGenerateKey(algorithm: types.EcKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { return null as any; } - public async onExportKey(format: types.KeyFormat, key: core.BaseCryptoKey, ...args: any[]): Promise { + public async onExportKey(format: types.KeyFormat, key: core.CryptoKey, ...args: any[]): Promise { return null as any; } - public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { return null as any; } } @@ -199,19 +199,19 @@ context("EC", () => { context("EdDSA", () => { class TestEdDsaProvider extends core.EdDsaProvider { - public async onSign(algorithm: types.EcdsaParams, key: core.BaseCryptoKey, data: ArrayBuffer, ...args: any[]): Promise { + public async onSign(algorithm: types.EcdsaParams, key: core.CryptoKey, data: ArrayBuffer, ...args: any[]): Promise { return null as any; } - public async onVerify(algorithm: types.EcdsaParams, key: core.BaseCryptoKey, signature: ArrayBuffer, data: ArrayBuffer, ...args: any[]): Promise { + public async onVerify(algorithm: types.EcdsaParams, key: core.CryptoKey, signature: ArrayBuffer, data: ArrayBuffer, ...args: any[]): Promise { return true; } public async onGenerateKey(algorithm: types.EcKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { return null as any; } - public onExportKey(format: types.KeyFormat, key: core.BaseCryptoKey, ...args: any[]): Promise { + public onExportKey(format: types.KeyFormat, key: core.CryptoKey, ...args: any[]): Promise { return null as any; } - public onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { + public onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { return null as any; } } diff --git a/packages/core/test/ed.spec.ts b/packages/core/test/ed.spec.ts index d28079e..ebc9188 100644 --- a/packages/core/test/ed.spec.ts +++ b/packages/core/test/ed.spec.ts @@ -1,7 +1,7 @@ import { AsnConvert, AsnSerializer } from "@peculiar/asn1-schema"; import * as assert from "assert"; import { Convert } from "pvtsutils"; -import * as schema from "packages/core/src/schema"; +import * as asn1 from "../src/schema/asn1"; context("ED", () => { @@ -10,11 +10,11 @@ context("ED", () => { it("spki - jwk", () => { const pem = "MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE="; - const keyInfo = AsnConvert.parse(Convert.FromBase64(pem), schema.PublicKeyInfo); - const key = new schema.EdPublicKey(keyInfo.publicKey); + const keyInfo = AsnConvert.parse(Convert.FromBase64(pem), asn1.PublicKeyInfo); + const key = new asn1.EdPublicKey(keyInfo.publicKey); const jwk = key.toJSON(); - const key2 = new schema.EdPublicKey(); + const key2 = new asn1.EdPublicKey(); key2.fromJSON(jwk); assert.strictEqual( Convert.ToBase64(AsnSerializer.serialize(key2)), @@ -27,12 +27,12 @@ context("ED", () => { it("without public key", () => { const pem = "MC4CAQAwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhC"; - const keyInfo = AsnConvert.parse(Convert.FromBase64(pem), schema.OneAsymmetricKey); + const keyInfo = AsnConvert.parse(Convert.FromBase64(pem), asn1.OneAsymmetricKey); assert.strictEqual(keyInfo.publicKey, undefined); - const key = AsnConvert.parse(keyInfo.privateKey, schema.EdPrivateKey); + const key = AsnConvert.parse(keyInfo.privateKey, asn1.EdPrivateKey); const jwk = key.toJSON(); - const key2 = new schema.EdPrivateKey(); + const key2 = new asn1.EdPrivateKey(); key2.fromJSON(jwk); assert.strictEqual( Convert.ToBase64(AsnSerializer.serialize(key2)), @@ -43,12 +43,12 @@ context("ED", () => { it("with public key", () => { const pem = "MHICAQEwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhCoB8wHQYKKoZIhvcNAQkJFDEPDA1DdXJkbGUgQ2hhaXJzgSEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE="; - const keyInfo = AsnConvert.parse(Convert.FromBase64(pem), schema.OneAsymmetricKey); + const keyInfo = AsnConvert.parse(Convert.FromBase64(pem), asn1.OneAsymmetricKey); assert.ok(keyInfo.publicKey); - const key = AsnConvert.parse(keyInfo.privateKey, schema.EdPrivateKey); + const key = AsnConvert.parse(keyInfo.privateKey, asn1.EdPrivateKey); const jwk = key.toJSON(); - const key2 = new schema.EdPrivateKey(); + const key2 = new asn1.EdPrivateKey(); key2.fromJSON(jwk); assert.strictEqual( Convert.ToBase64(AsnSerializer.serialize(key2)), diff --git a/packages/core/test/key.spec.ts b/packages/core/test/key.spec.ts index c61698d..4bb2b8f 100644 --- a/packages/core/test/key.spec.ts +++ b/packages/core/test/key.spec.ts @@ -5,10 +5,10 @@ context("CryptoKey", () => { context("isKeyType", () => { it("correct key type", () => { - assert.equal(core.BaseCryptoKey.isKeyType("secret"), true); + assert.equal(core.CryptoKey.isKeyType("secret"), true); }); it("incorrect key type", () => { - assert.equal(core.BaseCryptoKey.isKeyType("Secret"), false); + assert.equal(core.CryptoKey.isKeyType("Secret"), false); }); }); diff --git a/packages/core/test/provider.spec.ts b/packages/core/test/provider.spec.ts index 6e24728..17e8b1b 100644 --- a/packages/core/test/provider.spec.ts +++ b/packages/core/test/provider.spec.ts @@ -76,7 +76,7 @@ context("ProviderCrypto", () => { context("sign", () => { - const correctKey = core.BaseCryptoKey.create( + const correctKey = core.CryptoKey.create( { name: "custom-alg" }, "secret", false, @@ -108,7 +108,7 @@ context("ProviderCrypto", () => { await assert.rejects( crypto.sign( { name: "custom-alg" }, - {} as core.BaseCryptoKey, + {} as core.CryptoKey, new ArrayBuffer(0), ), TypeError, @@ -119,7 +119,7 @@ context("ProviderCrypto", () => { await assert.rejects( crypto.sign( { name: "custom-alg" }, - core.BaseCryptoKey.create( + core.CryptoKey.create( { name: "wrong" }, "secret", true, @@ -135,7 +135,7 @@ context("ProviderCrypto", () => { await assert.rejects( crypto.sign( { name: "custom-alg" }, - core.BaseCryptoKey.create( + core.CryptoKey.create( { name: "custom-alg" }, "secret", true, @@ -153,7 +153,7 @@ context("ProviderCrypto", () => { it("error if length is not multiple 8", () => { const algorithm: types.Algorithm = { name: "custom-alg" }; - const key = core.BaseCryptoKey.create(algorithm, "secret", false, ["deriveBits"]); + const key = core.CryptoKey.create(algorithm, "secret", false, ["deriveBits"]); assert.throws(() => { crypto.checkDeriveBits(algorithm, key, 7); }, core.OperationError); diff --git a/packages/core/test/schema/asn.spec.ts b/packages/core/test/schema/asn.spec.ts index 9e7c6c7..72c7ce2 100644 --- a/packages/core/test/schema/asn.spec.ts +++ b/packages/core/test/schema/asn.spec.ts @@ -22,17 +22,17 @@ context("ASN1", () => { }; it("parse", () => { - const keyInfo = AsnParser.parse(bytes, core.PrivateKeyInfo); - const key = AsnParser.parse(keyInfo.privateKey, core.RsaPrivateKey); + const keyInfo = AsnParser.parse(bytes, core.asn1.PrivateKeyInfo); + const key = AsnParser.parse(keyInfo.privateKey, core.asn1.RsaPrivateKey); const jsonKey = JsonSerializer.toJSON(key); assert.deepEqual(jsonKey, json); }); it("serialize", () => { - const key = JsonParser.fromJSON(json, { targetSchema: core.RsaPrivateKey }); + const key = JsonParser.fromJSON(json, { targetSchema: core.asn1.RsaPrivateKey }); - const keyInfo = new core.PrivateKeyInfo(); + const keyInfo = new core.asn1.PrivateKeyInfo(); keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1"; keyInfo.privateKeyAlgorithm.parameters = null; keyInfo.privateKey = AsnSerializer.serialize(key); @@ -52,17 +52,17 @@ context("ASN1", () => { }; it("parse", () => { - const keyInfo = AsnParser.parse(bytes, core.PublicKeyInfo); - const key = AsnParser.parse(keyInfo.publicKey, core.RsaPublicKey); + const keyInfo = AsnParser.parse(bytes, core.asn1.PublicKeyInfo); + const key = AsnParser.parse(keyInfo.publicKey, core.asn1.RsaPublicKey); const jsonKey = JsonSerializer.toJSON(key); assert.deepEqual(jsonKey, json); }); it("serialize", () => { - const key = JsonParser.fromJSON(json, { targetSchema: core.RsaPublicKey }); + const key = JsonParser.fromJSON(json, { targetSchema: core.asn1.RsaPublicKey }); - const keyInfo = new core.PublicKeyInfo(); + const keyInfo = new core.asn1.PublicKeyInfo(); keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1"; keyInfo.publicKeyAlgorithm.parameters = null; keyInfo.publicKey = AsnSerializer.serialize(key); @@ -87,20 +87,20 @@ context("ASN1", () => { }; it("parse", () => { - const keyInfo = AsnParser.parse(bytes, core.PrivateKeyInfo); - const key = AsnParser.parse(keyInfo.privateKey, core.EcPrivateKey); + const keyInfo = AsnParser.parse(bytes, core.asn1.PrivateKeyInfo); + const key = AsnParser.parse(keyInfo.privateKey, core.asn1.EcPrivateKey); const jsonKey = JsonSerializer.toJSON(key); assert.deepEqual(jsonKey, json); }); it("serialize", () => { - const keyInfo = new core.PrivateKeyInfo(); + const keyInfo = new core.asn1.PrivateKeyInfo(); keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.10045.2.1"; keyInfo.privateKeyAlgorithm.parameters = AsnSerializer.serialize( - new core.ObjectIdentifier("1.2.840.10045.3.1.7"), + new core.asn1.ObjectIdentifier("1.2.840.10045.3.1.7"), ); - const key = JsonParser.fromJSON(json, { targetSchema: core.EcPrivateKey }); + const key = JsonParser.fromJSON(json, { targetSchema: core.asn1.EcPrivateKey }); keyInfo.privateKey = AsnSerializer.serialize(key); const asnKeyInfo = Buffer.from(AsnSerializer.serialize(keyInfo)); diff --git a/packages/core/test/schema/ec_signature.spec.ts b/packages/core/test/schema/ec_signature.spec.ts index 73752d9..89ff159 100644 --- a/packages/core/test/schema/ec_signature.spec.ts +++ b/packages/core/test/schema/ec_signature.spec.ts @@ -1,7 +1,7 @@ import * as assert from "assert"; import { Convert } from "pvtsutils"; import { AsnSerializer, AsnParser } from "@peculiar/asn1-schema"; -import { EcDsaSignature } from "@peculiar/webcrypto-core"; +import * as core from "@peculiar/webcrypto-core"; interface IEcSignatureTestVector { name: string; @@ -49,7 +49,7 @@ context("ASN1", () => { context("From WebCrypto to DER", () => { vectors.forEach((vector) => { it(vector.name, () => { - const value = EcDsaSignature.fromWebCryptoSignature(Convert.FromHex(vector.webCrypto)); + const value = core.asn1.EcDsaSignature.fromWebCryptoSignature(Convert.FromHex(vector.webCrypto)); const der = AsnSerializer.serialize(value); assert.strictEqual(Convert.ToHex(der), vector.asn1); }); @@ -59,7 +59,7 @@ context("ASN1", () => { context("From DER to WebCrypto", () => { vectors.forEach((vector) => { it(vector.name, () => { - const value = AsnParser.parse(Convert.FromHex(vector.asn1), EcDsaSignature); + const value = AsnParser.parse(Convert.FromHex(vector.asn1), core.asn1.EcDsaSignature); const signature = value.toWebCryptoSignature(); assert.strictEqual(Convert.ToHex(signature), vector.webCrypto); }); diff --git a/packages/core/test/schema/ed_keys.spec.ts b/packages/core/test/schema/ed_keys.spec.ts index 600660a..68146cd 100644 --- a/packages/core/test/schema/ed_keys.spec.ts +++ b/packages/core/test/schema/ed_keys.spec.ts @@ -1,7 +1,7 @@ import * as assert from "assert"; import { AsnConvert } from "@peculiar/asn1-schema"; import { JsonSerializer } from "@peculiar/json-schema"; -import { CurvePrivateKey, idX25519, idX448, PrivateKeyInfo, PublicKeyInfo } from "@peculiar/webcrypto-core"; +import * as core from "@peculiar/webcrypto-core"; import { Convert } from "pvtsutils"; context("EdDSA and ECDH-ES keys", () => { @@ -10,9 +10,9 @@ context("EdDSA and ECDH-ES keys", () => { const b64 = "MEYCAQAwBQYDK2VvBDoEOPhm20uZC//c0wk1EEapNDcIIlgSGVxnWhwRJvT5K3+iwjtcyV2inuEihA5Soa5BO2OHh5leznW+"; const raw = Buffer.from(b64, "base64"); - const pki = AsnConvert.parse(raw, PrivateKeyInfo); - assert.strictEqual(pki.privateKeyAlgorithm.algorithm, idX448); - const privateKey = AsnConvert.parse(pki.privateKey, CurvePrivateKey); + const pki = AsnConvert.parse(raw, core.asn1.PrivateKeyInfo); + assert.strictEqual(pki.privateKeyAlgorithm.algorithm, core.asn1.idX448); + const privateKey = AsnConvert.parse(pki.privateKey, core.asn1.CurvePrivateKey); assert.deepStrictEqual(JsonSerializer.toJSON(privateKey), { d: "-GbbS5kL_9zTCTUQRqk0NwgiWBIZXGdaHBEm9Pkrf6LCO1zJXaKe4SKEDlKhrkE7Y4eHmV7Odb4" }); }); @@ -21,8 +21,8 @@ context("EdDSA and ECDH-ES keys", () => { const b64 = "MCowBQYDK2VuAyEAR-a_Z6rz2HuBXn7m7v_pjef6nHfCWSIObVWCTr5nxjg"; const raw = Convert.FromBase64Url(b64); - const spki = AsnConvert.parse(raw, PublicKeyInfo); - assert.strictEqual(spki.publicKeyAlgorithm.algorithm, idX25519); + const spki = AsnConvert.parse(raw, core.asn1.PublicKeyInfo); + assert.strictEqual(spki.publicKeyAlgorithm.algorithm, core.asn1.idX25519); assert.strictEqual(Convert.ToBase64Url(spki.publicKey), "R-a_Z6rz2HuBXn7m7v_pjef6nHfCWSIObVWCTr5nxjg"); }); diff --git a/packages/core/test/storage.spec.ts b/packages/core/test/storage.spec.ts index a1f9868..0362c68 100644 --- a/packages/core/test/storage.spec.ts +++ b/packages/core/test/storage.spec.ts @@ -5,37 +5,37 @@ import * as assert from "assert"; // tslint:disable:max-classes-per-file class RsaSsaProvider extends core.RsaSsaProvider { - public onSign(algorithm: core.RsaSsaParams, key: core.BaseCryptoKey, data: ArrayBuffer): Promise { + public onSign(algorithm: core.RsaSsaParams, key: core.CryptoKey, data: ArrayBuffer): Promise { throw new Error("Method not implemented."); } - public onVerify(algorithm: core.RsaSsaParams, key: core.BaseCryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise { + public onVerify(algorithm: core.RsaSsaParams, key: core.CryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise { throw new Error("Method not implemented."); } public onGenerateKey(algorithm: types.RsaHashedKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { throw new Error("Method not implemented."); } - public onExportKey(format: types.KeyFormat, key: core.BaseCryptoKey): Promise { + public onExportKey(format: types.KeyFormat, key: core.CryptoKey): Promise { throw new Error("Method not implemented."); } - public onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { throw new Error("Method not implemented."); } } class RsaOaepProvider extends core.RsaOaepProvider { - public onEncrypt(algorithm: types.RsaOaepParams, key: core.BaseCryptoKey, data: ArrayBuffer): Promise { + public onEncrypt(algorithm: types.RsaOaepParams, key: core.CryptoKey, data: ArrayBuffer): Promise { throw new Error("Method not implemented."); } - public onDecrypt(algorithm: types.RsaOaepParams, key: core.BaseCryptoKey, data: ArrayBuffer): Promise { + public onDecrypt(algorithm: types.RsaOaepParams, key: core.CryptoKey, data: ArrayBuffer): Promise { throw new Error("Method not implemented."); } public onGenerateKey(algorithm: types.RsaHashedKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { throw new Error("Method not implemented."); } - public onExportKey(format: types.KeyFormat, key: core.BaseCryptoKey): Promise { + public onExportKey(format: types.KeyFormat, key: core.CryptoKey): Promise { throw new Error("Method not implemented."); } - public onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { throw new Error("Method not implemented."); } } diff --git a/packages/core/test/subtle.spec.ts b/packages/core/test/subtle.spec.ts index 381cb2a..0c0523f 100644 --- a/packages/core/test/subtle.spec.ts +++ b/packages/core/test/subtle.spec.ts @@ -12,35 +12,35 @@ context("SubtleCrypto", () => { return new ArrayBuffer(0); } - public override async onGenerateKey(algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public override async onGenerateKey(algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { return key; } - public override async onSign(algorithm: types.Algorithm, sKey: core.BaseCryptoKey, data: ArrayBuffer): Promise { + public override async onSign(algorithm: types.Algorithm, sKey: core.CryptoKey, data: ArrayBuffer): Promise { return new ArrayBuffer(0); } - public override async onVerify(algorithm: types.Algorithm, sKey: core.BaseCryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise { + public override async onVerify(algorithm: types.Algorithm, sKey: core.CryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise { return true; } - public override async onEncrypt(algorithm: types.Algorithm, sKey: core.BaseCryptoKey, data: ArrayBuffer): Promise { + public override async onEncrypt(algorithm: types.Algorithm, sKey: core.CryptoKey, data: ArrayBuffer): Promise { return new ArrayBuffer(0); } - public override async onDecrypt(algorithm: types.Algorithm, sKey: core.BaseCryptoKey, data: ArrayBuffer): Promise { + public override async onDecrypt(algorithm: types.Algorithm, sKey: core.CryptoKey, data: ArrayBuffer): Promise { return new ArrayBuffer(0); } - public override async onDeriveBits(algorithm: types.Algorithm, sKey: core.BaseCryptoKey, length: number): Promise { + public override async onDeriveBits(algorithm: types.Algorithm, sKey: core.CryptoKey, length: number): Promise { return new ArrayBuffer(0); } - public override async onExportKey(format: types.KeyFormat, sKey: core.BaseCryptoKey): Promise { + public override async onExportKey(format: types.KeyFormat, sKey: core.CryptoKey): Promise { return new ArrayBuffer(0); } - public override async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public override async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { return key; } @@ -56,7 +56,7 @@ context("SubtleCrypto", () => { } const subtle = new TestSubtleCrypto(); - const key = new core.BaseCryptoKey(); + const key = new core.CryptoKey(); key.algorithm = { name: "TEST" }; key.type = "secret", key.usages = ["sign", "verify", "deriveKey", "deriveBits", "encrypt", "decrypt", "wrapKey", "unwrapKey"]; diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index 753d930..f3122d5 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -1,2 +1,2 @@ -export { BaseCryptoKey as CryptoKey } from "@peculiar/webcrypto-core"; +export { CryptoKey as CryptoKey } from "@peculiar/webcrypto-core"; export { Crypto } from "./crypto"; diff --git a/packages/node/src/keys/key.ts b/packages/node/src/keys/key.ts index 318e4d0..28c3b74 100644 --- a/packages/node/src/keys/key.ts +++ b/packages/node/src/keys/key.ts @@ -2,7 +2,7 @@ import { JsonProp, JsonPropTypes } from "@peculiar/json-schema"; import * as core from "@peculiar/webcrypto-core"; import * as types from "@peculiar/webcrypto-types"; -export class CryptoKey extends core.BaseCryptoKey { +export class CryptoKey extends core.CryptoKey { public data: Buffer = Buffer.alloc(0); public override algorithm: types.KeyAlgorithm = { name: "" }; diff --git a/packages/node/src/mechs/aes/aes_cbc.ts b/packages/node/src/mechs/aes/aes_cbc.ts index 47c94c8..6334adf 100644 --- a/packages/node/src/mechs/aes/aes_cbc.ts +++ b/packages/node/src/mechs/aes/aes_cbc.ts @@ -6,7 +6,7 @@ import { AesCryptoKey } from "./key"; export class AesCbcProvider extends core.AesCbcProvider { - public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await AesCrypto.generateKey( { name: this.name, @@ -30,12 +30,12 @@ export class AesCbcProvider extends core.AesCbcProvider { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); return setCryptoKey(key); } - public override checkCryptoKey(key: core.BaseCryptoKey, keyUsage?: types.KeyUsage) { + public override checkCryptoKey(key: core.CryptoKey, keyUsage?: types.KeyUsage) { super.checkCryptoKey(key, keyUsage); if (!(getCryptoKey(key) instanceof AesCryptoKey)) { throw new TypeError("key: Is not a AesCryptoKey"); diff --git a/packages/node/src/mechs/aes/aes_cmac.ts b/packages/node/src/mechs/aes/aes_cmac.ts index 48beb3d..1fab4c9 100644 --- a/packages/node/src/mechs/aes/aes_cmac.ts +++ b/packages/node/src/mechs/aes/aes_cmac.ts @@ -114,7 +114,7 @@ function aesCmac(key: Buffer, message: Buffer) { export class AesCmacProvider extends core.AesCmacProvider { - public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await AesCrypto.generateKey( { name: this.name, @@ -141,12 +141,12 @@ export class AesCmacProvider extends core.AesCmacProvider { } - public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); return setCryptoKey(res); } - public override checkCryptoKey(key: core.BaseCryptoKey, keyUsage?: types.KeyUsage) { + public override checkCryptoKey(key: core.CryptoKey, keyUsage?: types.KeyUsage) { super.checkCryptoKey(key, keyUsage); if (!(getCryptoKey(key) instanceof AesCryptoKey)) { throw new TypeError("key: Is not a AesCryptoKey"); diff --git a/packages/node/src/mechs/aes/aes_ctr.ts b/packages/node/src/mechs/aes/aes_ctr.ts index 5acf9b3..a0025e1 100644 --- a/packages/node/src/mechs/aes/aes_ctr.ts +++ b/packages/node/src/mechs/aes/aes_ctr.ts @@ -6,7 +6,7 @@ import { AesCryptoKey } from "./key"; export class AesCtrProvider extends core.AesCtrProvider { - public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await AesCrypto.generateKey( { name: this.name, @@ -30,12 +30,12 @@ export class AesCtrProvider extends core.AesCtrProvider { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[], ...args: any[]): Promise { const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); return setCryptoKey(res); } - public override checkCryptoKey(key: core.BaseCryptoKey, keyUsage?: types.KeyUsage) { + public override checkCryptoKey(key: core.CryptoKey, keyUsage?: types.KeyUsage) { super.checkCryptoKey(key, keyUsage); if (!(getCryptoKey(key) instanceof AesCryptoKey)) { throw new TypeError("key: Is not a AesCryptoKey"); diff --git a/packages/node/src/mechs/aes/aes_ecb.ts b/packages/node/src/mechs/aes/aes_ecb.ts index 0f3c01d..abf6d53 100644 --- a/packages/node/src/mechs/aes/aes_ecb.ts +++ b/packages/node/src/mechs/aes/aes_ecb.ts @@ -6,7 +6,7 @@ import { AesCryptoKey } from "./key"; export class AesEcbProvider extends core.AesEcbProvider { - public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await AesCrypto.generateKey( { name: this.name, @@ -30,12 +30,12 @@ export class AesEcbProvider extends core.AesEcbProvider { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); return setCryptoKey(res); } - public override checkCryptoKey(key: core.BaseCryptoKey, keyUsage?: types.KeyUsage) { + public override checkCryptoKey(key: core.CryptoKey, keyUsage?: types.KeyUsage) { super.checkCryptoKey(key, keyUsage); if (!(getCryptoKey(key) instanceof AesCryptoKey)) { throw new TypeError("key: Is not a AesCryptoKey"); diff --git a/packages/node/src/mechs/aes/aes_gcm.ts b/packages/node/src/mechs/aes/aes_gcm.ts index ca61d69..3d59c36 100644 --- a/packages/node/src/mechs/aes/aes_gcm.ts +++ b/packages/node/src/mechs/aes/aes_gcm.ts @@ -6,7 +6,7 @@ import { AesCryptoKey } from "./key"; export class AesGcmProvider extends core.AesGcmProvider { - public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await AesCrypto.generateKey( { name: this.name, @@ -30,12 +30,12 @@ export class AesGcmProvider extends core.AesGcmProvider { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); return setCryptoKey(res); } - public override checkCryptoKey(key: core.BaseCryptoKey, keyUsage?: types.KeyUsage) { + public override checkCryptoKey(key: core.CryptoKey, keyUsage?: types.KeyUsage) { super.checkCryptoKey(key, keyUsage); if (!(getCryptoKey(key) instanceof AesCryptoKey)) { throw new TypeError("key: Is not a AesCryptoKey"); diff --git a/packages/node/src/mechs/aes/aes_kw.ts b/packages/node/src/mechs/aes/aes_kw.ts index 9bd1c21..978337f 100644 --- a/packages/node/src/mechs/aes/aes_kw.ts +++ b/packages/node/src/mechs/aes/aes_kw.ts @@ -6,7 +6,7 @@ import { AesCryptoKey } from "./key"; export class AesKwProvider extends core.AesKwProvider { - public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onGenerateKey(algorithm: types.AesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const res = await AesCrypto.generateKey( { name: this.name, @@ -22,7 +22,7 @@ export class AesKwProvider extends core.AesKwProvider { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); return setCryptoKey(res); } @@ -35,7 +35,7 @@ export class AesKwProvider extends core.AesKwProvider { return AesCrypto.decrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); } - public override checkCryptoKey(key: core.BaseCryptoKey, keyUsage?: types.KeyUsage) { + public override checkCryptoKey(key: core.CryptoKey, keyUsage?: types.KeyUsage) { super.checkCryptoKey(key, keyUsage); if (!(getCryptoKey(key) instanceof AesCryptoKey)) { throw new TypeError("key: Is not a AesCryptoKey"); diff --git a/packages/node/src/mechs/des/des_cbc.ts b/packages/node/src/mechs/des/des_cbc.ts index 02097bf..c8b300d 100644 --- a/packages/node/src/mechs/des/des_cbc.ts +++ b/packages/node/src/mechs/des/des_cbc.ts @@ -13,7 +13,7 @@ export class DesCbcProvider extends core.DesProvider { public ivSize = 8; public name = "DES-CBC"; - public async onGenerateKey(algorithm: core.DesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onGenerateKey(algorithm: core.DesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await DesCrypto.generateKey( { name: this.name, @@ -37,7 +37,7 @@ export class DesCbcProvider extends core.DesProvider { return DesCrypto.exportKey(format, getCryptoKey(key) as DesCryptoKey); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await DesCrypto.importKey(format, keyData, { name: this.name, length: this.keySizeBits }, extractable, keyUsages); if (key.data.length !== (this.keySizeBits >> 3)) { throw new core.OperationError("keyData: Wrong key size"); diff --git a/packages/node/src/mechs/des/des_ede3_cbc.ts b/packages/node/src/mechs/des/des_ede3_cbc.ts index c4869f7..72da173 100644 --- a/packages/node/src/mechs/des/des_ede3_cbc.ts +++ b/packages/node/src/mechs/des/des_ede3_cbc.ts @@ -13,7 +13,7 @@ export class DesEde3CbcProvider extends core.DesProvider { public ivSize = 8; public name = "DES-EDE3-CBC"; - public async onGenerateKey(algorithm: core.DesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onGenerateKey(algorithm: core.DesKeyGenParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await DesCrypto.generateKey( { name: this.name, @@ -37,7 +37,7 @@ export class DesEde3CbcProvider extends core.DesProvider { return DesCrypto.exportKey(format, getCryptoKey(key) as DesCryptoKey); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await DesCrypto.importKey(format, keyData, { name: this.name, length: this.keySizeBits }, extractable, keyUsages); if (key.data.length !== (this.keySizeBits >> 3)) { throw new core.OperationError("keyData: Wrong key size"); diff --git a/packages/node/src/mechs/ec/crypto.ts b/packages/node/src/mechs/ec/crypto.ts index 4f71555..6fca393 100644 --- a/packages/node/src/mechs/ec/crypto.ts +++ b/packages/node/src/mechs/ec/crypto.ts @@ -1,7 +1,6 @@ import { AsnParser, AsnSerializer } from "@peculiar/asn1-schema"; import { JsonParser, JsonSerializer } from "@peculiar/json-schema"; import * as core from "@peculiar/webcrypto-core"; -import * as schema from "packages/core/src/schema"; import * as types from "@peculiar/webcrypto-types"; import * as crypto from "crypto"; import { BufferSourceConverter } from "pvtsutils"; @@ -63,7 +62,7 @@ export class EcCrypto { }; const signature = signer.sign(options); - const ecSignature = AsnParser.parse(signature, schema.EcDsaSignature); + const ecSignature = AsnParser.parse(signature, core.asn1.EcDsaSignature); const signatureRaw = core.EcUtils.encodeSignature(ecSignature, core.EcCurves.get(key.algorithm.namedCurve).size); @@ -82,7 +81,7 @@ export class EcCrypto { key: key.pem, }; - const ecSignature = new schema.EcDsaSignature(); + const ecSignature = new core.asn1.EcDsaSignature(); const namedCurve = core.EcCurves.get(key.algorithm.namedCurve); const signaturePoint = core.EcUtils.decodeSignature(signature, namedCurve.size); ecSignature.r = BufferSourceConverter.toArrayBuffer(signaturePoint.r); @@ -97,11 +96,11 @@ export class EcCrypto { const cryptoAlg = this.getOpenSSLNamedCurve((baseKey.algorithm as types.EcKeyAlgorithm).namedCurve); const ecdh = crypto.createECDH(cryptoAlg); - const asnPrivateKey = AsnParser.parse(baseKey.data, schema.PrivateKeyInfo); - const asnEcPrivateKey = AsnParser.parse(asnPrivateKey.privateKey, schema.EcPrivateKey); + const asnPrivateKey = AsnParser.parse(baseKey.data, core.asn1.PrivateKeyInfo); + const asnEcPrivateKey = AsnParser.parse(asnPrivateKey.privateKey, core.asn1.EcPrivateKey); ecdh.setPrivateKey(Buffer.from(asnEcPrivateKey.privateKey)); - const asnPublicKey = AsnParser.parse((algorithm.public as CryptoKey).data, schema.PublicKeyInfo); + const asnPublicKey = AsnParser.parse((algorithm.public as CryptoKey).data, core.asn1.PublicKeyInfo); const bits = ecdh.computeSecret(Buffer.from(asnPublicKey.publicKey)); return new Uint8Array(bits).buffer.slice(0, length >> 3); @@ -115,7 +114,7 @@ export class EcCrypto { case "spki": return new Uint8Array(key.data).buffer; case "raw": { - const publicKeyInfo = AsnParser.parse(key.data, schema.PublicKeyInfo); + const publicKeyInfo = AsnParser.parse(key.data, core.asn1.PublicKeyInfo); return publicKeyInfo.publicKey; } default: @@ -128,26 +127,26 @@ export class EcCrypto { case "jwk": { const jwk = keyData as types.JsonWebKey; if (jwk.d) { - const asnKey = JsonParser.fromJSON(keyData, { targetSchema: schema.EcPrivateKey }); + const asnKey = JsonParser.fromJSON(keyData, { targetSchema: core.asn1.EcPrivateKey }); return this.importPrivateKey(asnKey, algorithm, extractable, keyUsages); } else { - const asnKey = JsonParser.fromJSON(keyData, { targetSchema: schema.EcPublicKey }); + const asnKey = JsonParser.fromJSON(keyData, { targetSchema: core.asn1.EcPublicKey }); return this.importPublicKey(asnKey, algorithm, extractable, keyUsages); } } case "raw": { - const asnKey = new schema.EcPublicKey(keyData as ArrayBuffer); + const asnKey = new core.asn1.EcPublicKey(keyData as ArrayBuffer); return this.importPublicKey(asnKey, algorithm, extractable, keyUsages); } case "spki": { - const keyInfo = AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), schema.PublicKeyInfo); - const asnKey = new schema.EcPublicKey(keyInfo.publicKey); + const keyInfo = AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.asn1.PublicKeyInfo); + const asnKey = new core.asn1.EcPublicKey(keyInfo.publicKey); this.assertKeyParameters(keyInfo.publicKeyAlgorithm.parameters, algorithm.namedCurve); return this.importPublicKey(asnKey, algorithm, extractable, keyUsages); } case "pkcs8": { - const keyInfo = AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), schema.PrivateKeyInfo); - const asnKey = AsnParser.parse(keyInfo.privateKey, schema.EcPrivateKey); + const keyInfo = AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.asn1.PrivateKeyInfo); + const asnKey = AsnParser.parse(keyInfo.privateKey, core.asn1.EcPrivateKey); this.assertKeyParameters(keyInfo.privateKeyAlgorithm.parameters, algorithm.namedCurve); return this.importPrivateKey(asnKey, algorithm, extractable, keyUsages); } @@ -163,7 +162,7 @@ export class EcCrypto { let namedCurveIdentifier = ""; try { - namedCurveIdentifier = AsnParser.parse(parameters, schema.ObjectIdentifier).value; + namedCurveIdentifier = AsnParser.parse(parameters, core.asn1.ObjectIdentifier).value; } catch (e) { throw new core.CryptoError("Cannot read key info parameters"); } @@ -173,10 +172,10 @@ export class EcCrypto { } } - protected static async importPrivateKey(asnKey: schema.EcPrivateKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { - const keyInfo = new schema.PrivateKeyInfo(); + protected static async importPrivateKey(asnKey: core.asn1.EcPrivateKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { + const keyInfo = new core.asn1.PrivateKeyInfo(); keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.10045.2.1"; - keyInfo.privateKeyAlgorithm.parameters = AsnSerializer.serialize(new schema.ObjectIdentifier(getOidByNamedCurve(algorithm.namedCurve))); + keyInfo.privateKeyAlgorithm.parameters = AsnSerializer.serialize(new core.asn1.ObjectIdentifier(getOidByNamedCurve(algorithm.namedCurve))); keyInfo.privateKey = AsnSerializer.serialize(asnKey); const key = new EcPrivateKey(); @@ -189,11 +188,11 @@ export class EcCrypto { return key; } - protected static async importPublicKey(asnKey: schema.EcPublicKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { - const keyInfo = new schema.PublicKeyInfo(); + protected static async importPublicKey(asnKey: core.asn1.EcPublicKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { + const keyInfo = new core.asn1.PublicKeyInfo(); keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.10045.2.1"; const namedCurve = getOidByNamedCurve(algorithm.namedCurve); - keyInfo.publicKeyAlgorithm.parameters = AsnSerializer.serialize(new schema.ObjectIdentifier(namedCurve)); + keyInfo.publicKeyAlgorithm.parameters = AsnSerializer.serialize(new core.asn1.ObjectIdentifier(namedCurve)); keyInfo.publicKey = asnKey.value; const key = new EcPublicKey(); diff --git a/packages/node/src/mechs/ec/ec_dh.ts b/packages/node/src/mechs/ec/ec_dh.ts index 3d89253..6910483 100644 --- a/packages/node/src/mechs/ec/ec_dh.ts +++ b/packages/node/src/mechs/ec/ec_dh.ts @@ -29,7 +29,7 @@ export class EcdhProvider extends core.EcdhProvider { return EcCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await EcCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); return setCryptoKey(key); } diff --git a/packages/node/src/mechs/ec/ec_dsa.ts b/packages/node/src/mechs/ec/ec_dsa.ts index 14a926d..70c8e01 100644 --- a/packages/node/src/mechs/ec/ec_dsa.ts +++ b/packages/node/src/mechs/ec/ec_dsa.ts @@ -41,7 +41,7 @@ export class EcdsaProvider extends core.EcdsaProvider { return EcCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await EcCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); return setCryptoKey(key); } diff --git a/packages/node/src/mechs/ec/private_key.ts b/packages/node/src/mechs/ec/private_key.ts index 727de6f..9fede51 100644 --- a/packages/node/src/mechs/ec/private_key.ts +++ b/packages/node/src/mechs/ec/private_key.ts @@ -1,7 +1,6 @@ import * as asn1Schema from "@peculiar/asn1-schema"; import * as jsonSchema from "@peculiar/json-schema"; import * as core from "@peculiar/webcrypto-core"; -import * as schema from "packages/core/src/schema"; import * as types from "@peculiar/webcrypto-types"; import { AsymmetricKey } from "../../keys"; import { getOidByNamedCurve } from "./helper"; @@ -11,8 +10,8 @@ export class EcPrivateKey extends AsymmetricKey implements jsonSchema.IJsonConve public override algorithm!: types.EcKeyAlgorithm; public getKey() { - const keyInfo = asn1Schema.AsnParser.parse(this.data, schema.PrivateKeyInfo); - return asn1Schema.AsnParser.parse(keyInfo.privateKey, schema.EcPrivateKey); + const keyInfo = asn1Schema.AsnParser.parse(this.data, core.asn1.PrivateKeyInfo); + return asn1Schema.AsnParser.parse(keyInfo.privateKey, core.asn1.EcPrivateKey); } public toJSON() { @@ -33,12 +32,12 @@ export class EcPrivateKey extends AsymmetricKey implements jsonSchema.IJsonConve throw new core.OperationError(`Cannot get named curve from JWK. Property 'crv' is required`); } - const keyInfo = new schema.PrivateKeyInfo(); + const keyInfo = new core.asn1.PrivateKeyInfo(); keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.10045.2.1"; keyInfo.privateKeyAlgorithm.parameters = asn1Schema.AsnSerializer.serialize( - new schema.ObjectIdentifier(getOidByNamedCurve(json.crv)), + new core.asn1.ObjectIdentifier(getOidByNamedCurve(json.crv)), ); - const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: schema.EcPrivateKey }); + const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: core.asn1.EcPrivateKey }); keyInfo.privateKey = asn1Schema.AsnSerializer.serialize(key); this.data = Buffer.from(asn1Schema.AsnSerializer.serialize(keyInfo)); diff --git a/packages/node/src/mechs/ec/public_key.ts b/packages/node/src/mechs/ec/public_key.ts index b8825f1..135bfc1 100644 --- a/packages/node/src/mechs/ec/public_key.ts +++ b/packages/node/src/mechs/ec/public_key.ts @@ -12,8 +12,8 @@ export class EcPublicKey extends AsymmetricKey implements jsonSchema.IJsonConver public override algorithm!: types.EcKeyAlgorithm; public getKey() { - const keyInfo = asn1Schema.AsnParser.parse(this.data, schema.PublicKeyInfo); - return new schema.EcPublicKey(keyInfo.publicKey); + const keyInfo = asn1Schema.AsnParser.parse(this.data, core.asn1.PublicKeyInfo); + return new core.asn1.EcPublicKey(keyInfo.publicKey); } public toJSON() { @@ -34,12 +34,12 @@ export class EcPublicKey extends AsymmetricKey implements jsonSchema.IJsonConver throw new core.OperationError(`Cannot get named curve from JWK. Property 'crv' is required`); } - const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: schema.EcPublicKey }); + const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: core.asn1.EcPublicKey }); - const keyInfo = new schema.PublicKeyInfo(); + const keyInfo = new core.asn1.PublicKeyInfo(); keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.10045.2.1"; keyInfo.publicKeyAlgorithm.parameters = asn1Schema.AsnSerializer.serialize( - new schema.ObjectIdentifier(getOidByNamedCurve(json.crv)), + new core.asn1.ObjectIdentifier(getOidByNamedCurve(json.crv)), ); keyInfo.publicKey = asn1Schema.AsnSerializer.toASN(key).valueHex; diff --git a/packages/node/src/mechs/ed/crypto.ts b/packages/node/src/mechs/ed/crypto.ts index 275db29..dd31d1e 100644 --- a/packages/node/src/mechs/ed/crypto.ts +++ b/packages/node/src/mechs/ed/crypto.ts @@ -97,7 +97,7 @@ export class EdCrypto { case "spki": return new Uint8Array(key.data).buffer; case "raw": { - const publicKeyInfo = asn1Schema.AsnParser.parse(key.data, core.PublicKeyInfo); + const publicKeyInfo = asn1Schema.AsnParser.parse(key.data, core.asn1.PublicKeyInfo); return publicKeyInfo.publicKey; } default: @@ -110,7 +110,7 @@ export class EdCrypto { case "jwk": { const jwk = keyData as types.JsonWebKey; if (jwk.d) { - const asnKey = jsonSchema.JsonParser.fromJSON(keyData, { targetSchema: core.CurvePrivateKey }); + const asnKey = jsonSchema.JsonParser.fromJSON(keyData, { targetSchema: core.asn1.CurvePrivateKey }); return this.importPrivateKey(asnKey, algorithm, extractable, keyUsages); } else { if (!jwk.x) { @@ -123,12 +123,12 @@ export class EdCrypto { return this.importPublicKey(keyData as ArrayBuffer, algorithm, extractable, keyUsages); } case "spki": { - const keyInfo = asn1Schema.AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.PublicKeyInfo); + const keyInfo = asn1Schema.AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.asn1.PublicKeyInfo); return this.importPublicKey(keyInfo.publicKey, algorithm, extractable, keyUsages); } case "pkcs8": { - const keyInfo = asn1Schema.AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.PrivateKeyInfo); - const asnKey = asn1Schema.AsnParser.parse(keyInfo.privateKey, core.CurvePrivateKey); + const keyInfo = asn1Schema.AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.asn1.PrivateKeyInfo); + const asnKey = asn1Schema.AsnParser.parse(keyInfo.privateKey, core.asn1.CurvePrivateKey); return this.importPrivateKey(asnKey, algorithm, extractable, keyUsages); } default: @@ -136,7 +136,7 @@ export class EdCrypto { } } - protected static importPrivateKey(asnKey: core.CurvePrivateKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { + protected static importPrivateKey(asnKey: core.asn1.CurvePrivateKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { const key = new EdPrivateKey(); key.fromJSON({ crv: algorithm.namedCurve, diff --git a/packages/node/src/mechs/ed/ecdh_es.ts b/packages/node/src/mechs/ed/ecdh_es.ts index 39f0ef9..6bdbce2 100644 --- a/packages/node/src/mechs/ed/ecdh_es.ts +++ b/packages/node/src/mechs/ed/ecdh_es.ts @@ -21,7 +21,7 @@ export class EcdhEsProvider extends core.EcdhEsProvider { }; } - public async onDeriveBits(algorithm: types.EcdhKeyDeriveParams, baseKey: core.BaseCryptoKey, length: number): Promise { + public async onDeriveBits(algorithm: types.EcdhKeyDeriveParams, baseKey: core.CryptoKey, length: number): Promise { const bits = await EdCrypto.deriveBits({ ...algorithm, public: getCryptoKey(algorithm.public) }, getCryptoKey(baseKey), length); return bits; } @@ -30,7 +30,7 @@ export class EcdhEsProvider extends core.EcdhEsProvider { return EdCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await EdCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); return setCryptoKey(key); } diff --git a/packages/node/src/mechs/ed/eddsa.ts b/packages/node/src/mechs/ed/eddsa.ts index 7edae72..b0394c1 100644 --- a/packages/node/src/mechs/ed/eddsa.ts +++ b/packages/node/src/mechs/ed/eddsa.ts @@ -35,7 +35,7 @@ export class EdDsaProvider extends core.EdDsaProvider { return EdCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer | types.JsonWebKey, algorithm: types.EcKeyImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await EdCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); return setCryptoKey(key); } diff --git a/packages/node/src/mechs/ed/helper.ts b/packages/node/src/mechs/ed/helper.ts index a14c662..f19e31c 100644 --- a/packages/node/src/mechs/ed/helper.ts +++ b/packages/node/src/mechs/ed/helper.ts @@ -1,19 +1,18 @@ import * as core from "@peculiar/webcrypto-core"; -import * as schema from "packages/core/src/schema"; const edOIDs: { [key: string]: string; } = { // Ed448 - [schema.idEd448]: "Ed448", - "ed448": schema.idEd448, + [core.asn1.idEd448]: "Ed448", + "ed448": core.asn1.idEd448, // X448 - [schema.idX448]: "X448", - "x448": schema.idX448, + [core.asn1.idX448]: "X448", + "x448": core.asn1.idX448, // Ed25519 - [schema.idEd25519]: "Ed25519", - "ed25519": schema.idEd25519, + [core.asn1.idEd25519]: "Ed25519", + "ed25519": core.asn1.idEd25519, // X25519 - [schema.idX25519]: "X25519", - "x25519": schema.idX25519, + [core.asn1.idX25519]: "X25519", + "x25519": core.asn1.idX25519, }; export function getNamedCurveByOid(oid: string) { diff --git a/packages/node/src/mechs/ed/private_key.ts b/packages/node/src/mechs/ed/private_key.ts index 49ee44b..2d9df4e 100644 --- a/packages/node/src/mechs/ed/private_key.ts +++ b/packages/node/src/mechs/ed/private_key.ts @@ -11,8 +11,8 @@ export class EdPrivateKey extends AsymmetricKey implements jsonSchema.IJsonConve public override algorithm!: types.EcKeyAlgorithm; public getKey() { - const keyInfo = asn1Schema.AsnParser.parse(this.data, schema.PrivateKeyInfo); - return asn1Schema.AsnParser.parse(keyInfo.privateKey, schema.CurvePrivateKey); + const keyInfo = asn1Schema.AsnParser.parse(this.data, core.asn1.PrivateKeyInfo); + return asn1Schema.AsnParser.parse(keyInfo.privateKey, core.asn1.CurvePrivateKey); } public toJSON() { @@ -33,9 +33,9 @@ export class EdPrivateKey extends AsymmetricKey implements jsonSchema.IJsonConve throw new core.OperationError(`Cannot get named curve from JWK. Property 'crv' is required`); } - const keyInfo = new schema.PrivateKeyInfo(); + const keyInfo = new core.asn1.PrivateKeyInfo(); keyInfo.privateKeyAlgorithm.algorithm = getOidByNamedCurve(json.crv); - const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: schema.CurvePrivateKey }); + const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: core.asn1.CurvePrivateKey }); keyInfo.privateKey = asn1Schema.AsnSerializer.serialize(key); this.data = Buffer.from(asn1Schema.AsnSerializer.serialize(keyInfo)); diff --git a/packages/node/src/mechs/ed/public_key.ts b/packages/node/src/mechs/ed/public_key.ts index e4429db..fc98b73 100644 --- a/packages/node/src/mechs/ed/public_key.ts +++ b/packages/node/src/mechs/ed/public_key.ts @@ -13,7 +13,7 @@ export class EdPublicKey extends AsymmetricKey implements jsonSchema.IJsonConver public override algorithm!: types.EcKeyAlgorithm; public getKey() { - const keyInfo = asn1Schema.AsnParser.parse(this.data, schema.PublicKeyInfo); + const keyInfo = asn1Schema.AsnParser.parse(this.data, core.asn1.PublicKeyInfo); return keyInfo.publicKey; } @@ -40,7 +40,7 @@ export class EdPublicKey extends AsymmetricKey implements jsonSchema.IJsonConver throw new core.OperationError(`Cannot get property from JWK. Property 'x' is required`); } - const keyInfo = new schema.PublicKeyInfo(); + const keyInfo = new core.asn1.PublicKeyInfo(); keyInfo.publicKeyAlgorithm.algorithm = getOidByNamedCurve(json.crv); keyInfo.publicKey = pvtsutils.Convert.FromBase64Url(json.x); diff --git a/packages/node/src/mechs/hkdf/hkdf.ts b/packages/node/src/mechs/hkdf/hkdf.ts index e7c9d52..2213e26 100644 --- a/packages/node/src/mechs/hkdf/hkdf.ts +++ b/packages/node/src/mechs/hkdf/hkdf.ts @@ -7,7 +7,7 @@ import { HkdfCryptoKey } from "./key"; export class HkdfProvider extends core.HkdfProvider { - public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer, algorithm: types.HmacImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: ArrayBuffer, algorithm: types.HmacImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { if (format.toLowerCase() !== "raw") { throw new core.OperationError("Operation not supported"); } @@ -44,7 +44,7 @@ export class HkdfProvider extends core.HkdfProvider { return Buffer.concat(blocks).slice(0, byteLength); } - public override checkCryptoKey(key: core.BaseCryptoKey, keyUsage?: types.KeyUsage) { + public override checkCryptoKey(key: core.CryptoKey, keyUsage?: types.KeyUsage) { super.checkCryptoKey(key, keyUsage); if (!(getCryptoKey(key) instanceof HkdfCryptoKey)) { throw new TypeError("key: Is not HKDF CryptoKey"); diff --git a/packages/node/src/mechs/hmac/hmac.ts b/packages/node/src/mechs/hmac/hmac.ts index 5efd039..26b021f 100644 --- a/packages/node/src/mechs/hmac/hmac.ts +++ b/packages/node/src/mechs/hmac/hmac.ts @@ -8,7 +8,7 @@ import { HmacCryptoKey } from "./key"; export class HmacProvider extends core.HmacProvider { - public async onGenerateKey(algorithm: types.PreparedHashedAlgorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onGenerateKey(algorithm: types.PreparedHashedAlgorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const length = (algorithm.length || this.getDefaultLength(algorithm.hash.name)) >> 3 << 3; const key = new HmacCryptoKey(); key.algorithm = { @@ -39,7 +39,7 @@ export class HmacProvider extends core.HmacProvider { return hmac.compare(Buffer.from(signature)) === 0; } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.HmacImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.HmacImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { let key: HmacCryptoKey; switch (format.toLowerCase()) { diff --git a/packages/node/src/mechs/pbkdf/pbkdf2.ts b/packages/node/src/mechs/pbkdf/pbkdf2.ts index c18addb..b8dd789 100644 --- a/packages/node/src/mechs/pbkdf/pbkdf2.ts +++ b/packages/node/src/mechs/pbkdf/pbkdf2.ts @@ -21,7 +21,7 @@ export class Pbkdf2Provider extends core.Pbkdf2Provider { }); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.Algorithm, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { if (format === "raw") { const key = new PbkdfCryptoKey(); key.data = Buffer.from(keyData as ArrayBuffer); diff --git a/packages/node/src/mechs/rsa/crypto.ts b/packages/node/src/mechs/rsa/crypto.ts index 8ea7a56..4e4cd52 100644 --- a/packages/node/src/mechs/rsa/crypto.ts +++ b/packages/node/src/mechs/rsa/crypto.ts @@ -77,21 +77,21 @@ export class RsaCrypto { case "jwk": { const jwk = keyData as types.JsonWebKey; if (jwk.d) { - const asnKey = jsonSchema.JsonParser.fromJSON(keyData, { targetSchema: schema.RsaPrivateKey }); + const asnKey = jsonSchema.JsonParser.fromJSON(keyData, { targetSchema: core.asn1.RsaPrivateKey }); return this.importPrivateKey(asnKey, algorithm, extractable, keyUsages); } else { - const asnKey = jsonSchema.JsonParser.fromJSON(keyData, { targetSchema: schema.RsaPublicKey }); + const asnKey = jsonSchema.JsonParser.fromJSON(keyData, { targetSchema: core.asn1.RsaPublicKey }); return this.importPublicKey(asnKey, algorithm, extractable, keyUsages); } } case "spki": { - const keyInfo = asn1Schema.AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), schema.PublicKeyInfo); - const asnKey = asn1Schema.AsnParser.parse(keyInfo.publicKey, schema.RsaPublicKey); + const keyInfo = asn1Schema.AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.asn1.PublicKeyInfo); + const asnKey = asn1Schema.AsnParser.parse(keyInfo.publicKey, core.asn1.RsaPublicKey); return this.importPublicKey(asnKey, algorithm, extractable, keyUsages); } case "pkcs8": { - const keyInfo = asn1Schema.AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), schema.PrivateKeyInfo); - const asnKey = asn1Schema.AsnParser.parse(keyInfo.privateKey, schema.RsaPrivateKey); + const keyInfo = asn1Schema.AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.asn1.PrivateKeyInfo); + const asnKey = asn1Schema.AsnParser.parse(keyInfo.privateKey, core.asn1.RsaPrivateKey); return this.importPrivateKey(asnKey, algorithm, extractable, keyUsages); } default: @@ -137,8 +137,8 @@ export class RsaCrypto { } } - protected static importPrivateKey(asnKey: schema.RsaPrivateKey, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { - const keyInfo = new schema.PrivateKeyInfo(); + protected static importPrivateKey(asnKey: core.asn1.RsaPrivateKey, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { + const keyInfo = new core.asn1.PrivateKeyInfo(); keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1"; keyInfo.privateKeyAlgorithm.parameters = null; keyInfo.privateKey = asn1Schema.AsnSerializer.serialize(asnKey); @@ -155,8 +155,8 @@ export class RsaCrypto { return key; } - protected static importPublicKey(asnKey: schema.RsaPublicKey, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { - const keyInfo = new schema.PublicKeyInfo(); + protected static importPublicKey(asnKey: core.asn1.RsaPublicKey, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]) { + const keyInfo = new core.asn1.PublicKeyInfo(); keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1"; keyInfo.publicKeyAlgorithm.parameters = null; keyInfo.publicKey = asn1Schema.AsnSerializer.serialize(asnKey); diff --git a/packages/node/src/mechs/rsa/private_key.ts b/packages/node/src/mechs/rsa/private_key.ts index cb693a7..b1db317 100644 --- a/packages/node/src/mechs/rsa/private_key.ts +++ b/packages/node/src/mechs/rsa/private_key.ts @@ -10,8 +10,8 @@ export class RsaPrivateKey extends AsymmetricKey { public override algorithm!: types.RsaHashedKeyAlgorithm; public getKey() { - const keyInfo = asn1Schema.AsnParser.parse(this.data, core.PrivateKeyInfo); - return asn1Schema.AsnParser.parse(keyInfo.privateKey, core.RsaPrivateKey); + const keyInfo = asn1Schema.AsnParser.parse(this.data, core.asn1.PrivateKeyInfo); + return asn1Schema.AsnParser.parse(keyInfo.privateKey, core.asn1.RsaPrivateKey); } public toJSON() { @@ -28,9 +28,9 @@ export class RsaPrivateKey extends AsymmetricKey { } public fromJSON(json: types.JsonWebKey) { - const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: core.RsaPrivateKey }); + const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: core.asn1.RsaPrivateKey }); - const keyInfo = new core.PrivateKeyInfo(); + const keyInfo = new core.asn1.PrivateKeyInfo(); keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1"; keyInfo.privateKeyAlgorithm.parameters = null; keyInfo.privateKey = asn1Schema.AsnSerializer.serialize(key); diff --git a/packages/node/src/mechs/rsa/public_key.ts b/packages/node/src/mechs/rsa/public_key.ts index d7d8a1b..2056b76 100644 --- a/packages/node/src/mechs/rsa/public_key.ts +++ b/packages/node/src/mechs/rsa/public_key.ts @@ -10,8 +10,8 @@ export class RsaPublicKey extends AsymmetricKey { public override algorithm!: types.RsaHashedKeyAlgorithm; public getKey() { - const keyInfo = asn1Schema.AsnParser.parse(this.data, core.PublicKeyInfo); - return asn1Schema.AsnParser.parse(keyInfo.publicKey, core.RsaPublicKey); + const keyInfo = asn1Schema.AsnParser.parse(this.data, core.asn1.PublicKeyInfo); + return asn1Schema.AsnParser.parse(keyInfo.publicKey, core.asn1.RsaPublicKey); } public toJSON() { @@ -28,9 +28,9 @@ export class RsaPublicKey extends AsymmetricKey { } public fromJSON(json: types.JsonWebKey) { - const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: core.RsaPublicKey }); + const key = jsonSchema.JsonParser.fromJSON(json, { targetSchema: core.asn1.RsaPublicKey }); - const keyInfo = new core.PublicKeyInfo(); + const keyInfo = new core.asn1.PublicKeyInfo(); keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1"; keyInfo.publicKeyAlgorithm.parameters = null; keyInfo.publicKey = asn1Schema.AsnSerializer.serialize(key); diff --git a/packages/node/src/mechs/rsa/rsa_oaep.ts b/packages/node/src/mechs/rsa/rsa_oaep.ts index a1747c1..b152ec0 100644 --- a/packages/node/src/mechs/rsa/rsa_oaep.ts +++ b/packages/node/src/mechs/rsa/rsa_oaep.ts @@ -148,7 +148,7 @@ export class RsaOaepProvider extends core.RsaOaepProvider { return RsaCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await RsaCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); return setCryptoKey(key); } diff --git a/packages/node/src/mechs/rsa/rsa_pss.ts b/packages/node/src/mechs/rsa/rsa_pss.ts index fa4a3d6..17f4a1a 100644 --- a/packages/node/src/mechs/rsa/rsa_pss.ts +++ b/packages/node/src/mechs/rsa/rsa_pss.ts @@ -39,7 +39,7 @@ export class RsaPssProvider extends core.RsaPssProvider { return RsaCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await RsaCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); return setCryptoKey(key); } diff --git a/packages/node/src/mechs/rsa/rsa_ssa.ts b/packages/node/src/mechs/rsa/rsa_ssa.ts index 5d4f3eb..271d64a 100644 --- a/packages/node/src/mechs/rsa/rsa_ssa.ts +++ b/packages/node/src/mechs/rsa/rsa_ssa.ts @@ -39,7 +39,7 @@ export class RsaSsaProvider extends core.RsaSsaProvider { return RsaCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { + public async onImportKey(format: types.KeyFormat, keyData: types.JsonWebKey | ArrayBuffer, algorithm: types.RsaHashedImportParams, extractable: boolean, keyUsages: types.KeyUsage[]): Promise { const key = await RsaCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); return setCryptoKey(key); } diff --git a/packages/node/src/mechs/storage.ts b/packages/node/src/mechs/storage.ts index ea9ff62..d865415 100644 --- a/packages/node/src/mechs/storage.ts +++ b/packages/node/src/mechs/storage.ts @@ -14,7 +14,7 @@ export function getCryptoKey(key: types.CryptoKey) { } export function setCryptoKey(value: InternalCryptoKey) { - const key = core.BaseCryptoKey.create(value.algorithm, value.type, value.extractable, value.usages); + const key = core.CryptoKey.create(value.algorithm, value.type, value.extractable, value.usages); Object.freeze(key); keyStorage.set(key, value); diff --git a/packages/test/src/index.ts b/packages/test/src/index.ts index 2c6b04c..1baceeb 100644 --- a/packages/test/src/index.ts +++ b/packages/test/src/index.ts @@ -1,2 +1,3 @@ +export * from "./types"; export * from "./webcrypto_test"; export * as vectors from "./vectors"; diff --git a/packages/types/test/types.spec.ts b/packages/types/test/types.spec.ts new file mode 100644 index 0000000..42f3e0c --- /dev/null +++ b/packages/types/test/types.spec.ts @@ -0,0 +1,15 @@ +// Test allows to validate that WebCrypto API from @peculiar/webcrypto-types matches to DOM WebCrypto +// NOTE: This file should be in 'exclude' option of tsconfig.json file + +/// + +import * as types from "../src"; + +context("Types", () => { + + it("@peculiar WebCrypto matches DOM WebCrypto", () => { + const myCrypto = {} as types.Crypto; + let crypto: globalThis.Crypto = myCrypto; + }); + +}); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index af1db7e..da9c87d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,6 +18,10 @@ "@peculiar/webcrypto-pkcs11": [ "./packages/pkcs11/src" ], - } - } + }, + "skipLibCheck": true, // TODO @peculiar/x509 should use @peculiar/webcrypto-types. Remove this line when it's fixed + }, + "exclude": [ + "packages/types/test/types.spec.ts" + ] } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 2efcb7b..e55a3fc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -85,12 +85,110 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" -"@peculiar/asn1-schema@^2.1.7": - version "2.1.7" - resolved "https://registry.yarnpkg.com/@peculiar/asn1-schema/-/asn1-schema-2.1.7.tgz#17d9b79f7df748ef84158ea61a24fc11d9f3f4d9" - integrity sha512-RxnHrl7WgNxWPsGeSw7YUGTS3s5En7JwhdOilRpEShQJQ9TH8v1nZtslyZGX5GxgUJ1P4dnRsCnt7R5EYmvtUg== +"@peculiar/asn1-cms@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-cms/-/asn1-cms-2.1.8.tgz#0f2591e46f9ee360c1f834ff191b44be49dbfc6c" + integrity sha512-xWW1thg1V+SowpYkzuJsQdKg0n/myfwxajgjXKXyxYbIUqer4+2wRAA8lz/0//Lt1cyuG/3slaewncus9QM3Yw== dependencies: - asn1js "^3.0.3" + "@peculiar/asn1-schema" "^2.1.8" + "@peculiar/asn1-x509" "^2.1.8" + "@peculiar/asn1-x509-attr" "^2.1.8" + asn1js "^3.0.4" + tslib "^2.4.0" + +"@peculiar/asn1-csr@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-csr/-/asn1-csr-2.1.8.tgz#72948e87f69c8645bf379b591260a084e61f5d7c" + integrity sha512-orsY1nJ/2fopMabioLdXBZeuxlXAEkXfqBl6hEYHUG9Ia5uoAxnT5yxmn78RvbcdDqp9GBSmh653zGyYX8nuSg== + dependencies: + "@peculiar/asn1-schema" "^2.1.8" + "@peculiar/asn1-x509" "^2.1.8" + asn1js "^3.0.4" + tslib "^2.4.0" + +"@peculiar/asn1-ecc@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-ecc/-/asn1-ecc-2.1.8.tgz#3ad6d5ab44cbf2ee0ec6bcaa2575c8e9c1148947" + integrity sha512-MoIWxTP/USioOSwjDvUJL2esWzZkrYdlsLQfcWUOn5jVLe+q1nSHBubBjhXtjR9iIN0LnXQ629nTNdhLznx+jQ== + dependencies: + "@peculiar/asn1-schema" "^2.1.8" + "@peculiar/asn1-x509" "^2.1.8" + asn1js "^3.0.4" + tslib "^2.4.0" + +"@peculiar/asn1-pfx@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-pfx/-/asn1-pfx-2.1.8.tgz#1d6798c78493d23ce68d7cec0169049447e73606" + integrity sha512-iX5vI9unoR0dT3ME1SgKSHGzsGowTVOolzSz68+jRV3B6nSJljHdiriVyUXIz+pGFSShRPPgy+xagGmo9A9cYg== + dependencies: + "@peculiar/asn1-cms" "^2.1.8" + "@peculiar/asn1-pkcs8" "^2.1.8" + "@peculiar/asn1-rsa" "^2.1.8" + "@peculiar/asn1-schema" "^2.1.8" + asn1js "^3.0.4" + tslib "^2.4.0" + +"@peculiar/asn1-pkcs8@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-pkcs8/-/asn1-pkcs8-2.1.8.tgz#997cbf35f37ee661e723e96750b944f6703d57c7" + integrity sha512-2WbAoDQP8Gs0mtyzXlYbSvJ4QPiYuBNasIfpkRV4fjNNtCh80O3gdONa4B5xWWGhLbitjDE68ZigwrXlVACr1g== + dependencies: + "@peculiar/asn1-schema" "^2.1.8" + "@peculiar/asn1-x509" "^2.1.8" + asn1js "^3.0.4" + tslib "^2.4.0" + +"@peculiar/asn1-pkcs9@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-pkcs9/-/asn1-pkcs9-2.1.8.tgz#c581ece374056e029d4c719ea733a6e54b49341f" + integrity sha512-VoSWvUalTtnXpnDXItGpcWVNn5RI2iZ+poDMUEYg3qmHd9V6c/ieI3jmMbSPKlPq4uiG6BxfZwCCbDAyDa70vg== + dependencies: + "@peculiar/asn1-cms" "^2.1.8" + "@peculiar/asn1-pfx" "^2.1.8" + "@peculiar/asn1-pkcs8" "^2.1.8" + "@peculiar/asn1-schema" "^2.1.8" + "@peculiar/asn1-x509" "^2.1.8" + "@peculiar/asn1-x509-attr" "^2.1.8" + asn1js "^3.0.4" + tslib "^2.4.0" + +"@peculiar/asn1-rsa@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-rsa/-/asn1-rsa-2.1.8.tgz#26d46e83df53095387fa9d3ae5fde061f9f64590" + integrity sha512-p3RuAmbetJUy7hAOwkBsGp4vULjrpSQFZvz9OT83bkaOVHOGTzpc42Q02869hIX5eAB3LdZqct+atcpaHR6c/w== + dependencies: + "@peculiar/asn1-schema" "^2.1.8" + "@peculiar/asn1-x509" "^2.1.8" + asn1js "^3.0.4" + tslib "^2.4.0" + +"@peculiar/asn1-schema@^2.1.7", "@peculiar/asn1-schema@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-schema/-/asn1-schema-2.1.8.tgz#552300a1ed7991b22c9abf789a3920a3cb94c26b" + integrity sha512-u34H/bpqCdDuqrCVZvH0vpwFBT/dNEdNY+eE8u4IuC26yYnhDkXF4+Hliqca88Avbb7hyN2EF/eokyDdyS7G/A== + dependencies: + asn1js "^3.0.4" + pvtsutils "^1.3.2" + tslib "^2.4.0" + +"@peculiar/asn1-x509-attr@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-x509-attr/-/asn1-x509-attr-2.1.8.tgz#c55d4ccfc3c1ce11e0f158f1e2ad9326961fe650" + integrity sha512-JtSjcCLOXILesj6lC3kqe2qVvRcTcWLozjO94cDVKWtLX04/QiwZbhetG9OPLaE0DFfErUz1/DC06swAO4yGRQ== + dependencies: + "@peculiar/asn1-schema" "^2.1.8" + "@peculiar/asn1-x509" "^2.1.8" + asn1js "^3.0.4" + tslib "^2.4.0" + +"@peculiar/asn1-x509@^2.1.8": + version "2.1.8" + resolved "https://registry.yarnpkg.com/@peculiar/asn1-x509/-/asn1-x509-2.1.8.tgz#b67317ba1ee33c758ad7c6145dbaa1ddef4f1913" + integrity sha512-asAcoeZ+bjy/4/lf6gbMlfmywHpxLBa7LBE4pPCzSAKBM0IHXWa7bqsDyshtywzLW+VpA+G2m0Fs7Lt7Woh7RA== + dependencies: + "@peculiar/asn1-schema" "^2.1.8" + asn1js "^3.0.4" + ipaddr.js "^2.0.1" pvtsutils "^1.3.2" tslib "^2.4.0" @@ -101,6 +199,23 @@ dependencies: tslib "^2.0.0" +"@peculiar/x509@^1.6.4": + version "1.6.4" + resolved "https://registry.yarnpkg.com/@peculiar/x509/-/x509-1.6.4.tgz#f36567540fff082411425cc839abae38d3d2fcf6" + integrity sha512-sj9YN5LrAOyrn3lWU8KxsTQ87spVGyjypDchJrZEi8W4jiQxLoBNvFwk/D4KWNklYrgzvdoQQKIlm8Md975iQQ== + dependencies: + "@peculiar/asn1-cms" "^2.1.8" + "@peculiar/asn1-csr" "^2.1.8" + "@peculiar/asn1-ecc" "^2.1.8" + "@peculiar/asn1-pkcs9" "^2.1.8" + "@peculiar/asn1-rsa" "^2.1.8" + "@peculiar/asn1-schema" "^2.1.8" + "@peculiar/asn1-x509" "^2.1.8" + pvtsutils "^1.3.2" + reflect-metadata "^0.1.13" + tslib "^2.4.0" + tsyringe "^4.6.0" + "@rollup/pluginutils@^4.1.2": version "4.2.1" resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-4.2.1.tgz#e6c6c3aba0744edce3fb2074922d3776c0af2a6d" @@ -315,10 +430,10 @@ array-union@^2.1.0: resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d" integrity sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw== -asn1js@^3.0.3: - version "3.0.3" - resolved "https://registry.yarnpkg.com/asn1js/-/asn1js-3.0.3.tgz#fbecdcced2c046552db0c63af284eccc494e0e74" - integrity sha512-Oz62ld3K3n6KlRUr4iZSMbSFDDPCW84G1yX7b/gWK5a0Ka7ZYOmCxFpu91lFdQygkBi2Hgoft1/WxRqFrONqLA== +asn1js@^3.0.4: + version "3.0.5" + resolved "https://registry.yarnpkg.com/asn1js/-/asn1js-3.0.5.tgz#5ea36820443dbefb51cc7f88a2ebb5b462114f38" + integrity sha512-FVnvrKJwpt9LP2lAMl8qZswRNm3T4q9CON+bxldk2iwk3FFpuwhx2FfinyitizWHsVYyaY+y5JzDR0rCMV5yTQ== dependencies: pvtsutils "^1.3.2" pvutils "^1.1.3" @@ -890,6 +1005,11 @@ inherits@2: resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== +ipaddr.js@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-2.0.1.tgz#eca256a7a877e917aeb368b0a7497ddf42ef81c0" + integrity sha512-1qTgH9NG+IIJ4yfKs2e6Pp1bZg8wbDbKHT21HrLIeYBTRLgMYKnMTPAuI3Lcs61nfx5h1xlXnbJtH1kX5/d/ng== + is-binary-path@~2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/is-binary-path/-/is-binary-path-2.1.0.tgz#ea1f7f3b80f064236e83470f86c09c254fb45b09" @@ -1297,6 +1417,11 @@ readdirp@~3.6.0: dependencies: picomatch "^2.2.1" +reflect-metadata@^0.1.13: + version "0.1.13" + resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08" + integrity sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg== + regexpp@^3.2.0: version "3.2.0" resolved "https://registry.yarnpkg.com/regexpp/-/regexpp-3.2.0.tgz#0425a2768d8f23bad70ca4b90461fa2f1213e1b2" @@ -1507,7 +1632,7 @@ tsconfig-paths@^3.14.1: minimist "^1.2.6" strip-bom "^3.0.0" -tslib@^1.8.1: +tslib@^1.8.1, tslib@^1.9.3: version "1.14.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== @@ -1524,6 +1649,13 @@ tsutils@^3.21.0: dependencies: tslib "^1.8.1" +tsyringe@^4.6.0: + version "4.6.0" + resolved "https://registry.yarnpkg.com/tsyringe/-/tsyringe-4.6.0.tgz#14915d3d7f0db35e1cf7269bdbf7c440713c8d07" + integrity sha512-BMQAZamSfEmIQzH8WJeRu1yZGQbPSDuI9g+yEiKZFIcO46GPZuMOC2d0b52cVBdw1d++06JnDSIIZvEnogMdAw== + dependencies: + tslib "^1.9.3" + type-check@^0.4.0, type-check@~0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"