Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Enhance EdAlgorithm support #77

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@
]
},
"devDependencies": {
"@babel/core": "^7.24.5",
"@babel/preset-env": "^7.24.5",
"@peculiar/webcrypto": "^1.4.6",
"@babel/core": "^7.24.6",
"@babel/preset-env": "^7.24.6",
"@peculiar/webcrypto": "^1.5.0",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-commonjs": "^25.0.8",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"@types/mocha": "^10.0.6",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "^7.10.0",
"@typescript-eslint/eslint-plugin": "^7.11.0",
"@typescript-eslint/parser": "^7.11.0",
"coveralls": "^3.1.1",
"eslint": "8.57.0",
"mocha": "^10.4.0",
"nyc": "^15.1.0",
"rimraf": "^5.0.7",
"rollup": "^4.17.2",
"rollup": "^4.18.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-typescript2": "^0.36.0",
"ts-node": "^10.9.2",
Expand Down
16 changes: 13 additions & 3 deletions src/ed_algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ export class EdAlgorithm implements IAlgorithm {
public toAsnAlgorithm(alg: EcKeyGenParams): AlgorithmIdentifier | null {
let algorithm: string | null = null;
switch (alg.name.toLowerCase()) {
case "ed25519":
// This algorithm is supported by WebCrypto API
algorithm = idEd25519;
break;
case "x25519":
// This algorithm is supported by WebCrypto API
algorithm = idX25519;
break;
case "eddsa":
// This algorithm works with @peculiar/webcrypto only
switch (alg.namedCurve.toLowerCase()) {
case "ed25519":
algorithm = idEd25519;
Expand All @@ -32,6 +41,7 @@ export class EdAlgorithm implements IAlgorithm {
}
break;
case "ecdh-es":
// This algorithm works with @peculiar/webcrypto only
switch (alg.namedCurve.toLowerCase()) {
case "x25519":
algorithm = idX25519;
Expand All @@ -50,14 +60,14 @@ export class EdAlgorithm implements IAlgorithm {
return null;
}

public toWebAlgorithm(alg: AlgorithmIdentifier): HashedAlgorithm | EcKeyGenParams | null {
public toWebAlgorithm(alg: AlgorithmIdentifier): HashedAlgorithm | EcKeyGenParams | Algorithm | null {
switch (alg.algorithm) {
case idEd25519:
return { name: "EdDSA", namedCurve: "Ed25519" };
return { name: "Ed25519" };
case idEd448:
return { name: "EdDSA", namedCurve: "Ed448" };
case idX25519:
return { name: "ECDH-ES", namedCurve: "X25519" };
return { name: "X25519" };
case idX448:
return { name: "ECDH-ES", namedCurve: "X448" };
}
Expand Down
26 changes: 26 additions & 0 deletions test/attribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as assert from "node:assert";
import { Attribute, TextObject } from "../src";

context("Attribute", function () {
context("#toTextObject()", function () {
it("should return a TextObject with correct values", function () {
const attribute = new Attribute("1.2.3", [new ArrayBuffer(8)]);
const textObject = attribute.toTextObject();

assert.strictEqual(textObject[TextObject.NAME], "1.2.3");
assert.ok(Array.isArray(textObject.Value));
assert.strictEqual(textObject.Value.length, 1);
assert(textObject.Value[0] instanceof TextObject);
});
});

context("#toTextObjectWithoutValue()", function () {
it("should return a TextObject without the Value property", function () {
const attribute = new Attribute("1.2.3", [new ArrayBuffer(8)]);
const textObject = attribute.toTextObjectWithoutValue();

assert.strictEqual(textObject[TextObject.NAME], "1.2.3");
assert.strictEqual(textObject.Value, undefined);
});
});
});
6 changes: 3 additions & 3 deletions test/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ D314IEOg4mnS8Q==
await x509.SubjectKeyIdentifierExtension.create(keys.publicKey),
]
});
console.log(cert.toString("pem"));
// console.log(cert.toString("pem"));
const ok = await cert.verify({ date: new Date("2020/01/01 12:00") });
assert.strictEqual(ok, true);
});
Expand Down Expand Up @@ -762,7 +762,7 @@ ZYYG
const ok = await cert.verify({
signatureOnly: true,
});
assert.deepStrictEqual(cert.signatureAlgorithm, { name: "EdDSA", namedCurve: "Ed25519" });
assert.deepStrictEqual(cert.signatureAlgorithm, { name: "Ed25519" });
assert.strictEqual(ok, true);
});

Expand Down Expand Up @@ -880,7 +880,7 @@ ZYYG
});
assert.strictEqual(ok, true);

console.log(crl.toString("text"));
// console.log(crl.toString("text"));
});

});
Expand Down
140 changes: 140 additions & 0 deletions test/ec_algorithm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import * as assert from "assert";
import { EcAlgorithm, HashedAlgorithm } from "../src";
import { AlgorithmIdentifier } from "@peculiar/asn1-x509";
import { ECParameters, id_ecPublicKey, id_secp256r1 } from "@peculiar/asn1-ecc";
import { AsnConvert } from "@peculiar/asn1-schema";

context("EcAlgorithm", () => {
let ecAlgorithm: EcAlgorithm;

beforeEach(() => {
ecAlgorithm = new EcAlgorithm();
});

const testVectors: {
asnAlgorithm: string;
webAlgorithm: HashedAlgorithm | EcKeyGenParams;
}[] = [
{
asnAlgorithm: "1.2.840.10045.4.1",
webAlgorithm: { name: "ECDSA", hash: { name: "SHA-1" } }
},
{
asnAlgorithm: "1.2.840.10045.4.3.2",
webAlgorithm: { name: "ECDSA", hash: { name: "SHA-256" } }
},
{
asnAlgorithm: "1.2.840.10045.4.3.3",
webAlgorithm: { name: "ECDSA", hash: { name: "SHA-384" } }
},
{
asnAlgorithm: "1.2.840.10045.4.3.4",
webAlgorithm: { name: "ECDSA", hash: { name: "SHA-512" } }
},
{
asnAlgorithm: id_secp256r1,
webAlgorithm: { name: "ECDSA", namedCurve: "P-256" }
},
{
asnAlgorithm: "1.3.132.0.10",
webAlgorithm: { name: "ECDSA", namedCurve: "K-256" }
},
{
asnAlgorithm: "1.3.132.0.34",
webAlgorithm: { name: "ECDSA", namedCurve: "P-384" }
},
{
asnAlgorithm: "1.3.132.0.35",
webAlgorithm: { name: "ECDSA", namedCurve: "P-521" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.1",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP160r1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.2",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP160t1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.3",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP192r1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.4",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP192t1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.5",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP224r1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.6",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP224t1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.7",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP256r1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.8",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP256t1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.9",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP320r1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.10",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP320t1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.11",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP384r1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.12",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP384t1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.13",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP512r1" }
},
{
asnAlgorithm: "1.3.36.3.3.2.8.1.1.14",
webAlgorithm: { name: "ECDSA", namedCurve: "brainpoolP512t1" }
},
];


testVectors.forEach(({ asnAlgorithm, webAlgorithm }) => {
const withValue = "hash" in webAlgorithm ? webAlgorithm.hash.name : webAlgorithm.namedCurve;

context(`Algorithm ${webAlgorithm.name} with ${withValue}`, () => {

it("#toAsnAlgorithm()", () => {
const result = ecAlgorithm.toAsnAlgorithm(webAlgorithm);
assert.ok(result);
if ("hash" in webAlgorithm) {
assert.strictEqual(result.algorithm, asnAlgorithm);
} else {
assert.strictEqual(result.algorithm, id_ecPublicKey);
assert.ok(result.parameters);
const asnParameters = AsnConvert.parse(result.parameters, ECParameters);
assert.strictEqual(asnParameters.namedCurve, asnAlgorithm);
}
});

it("#toWebAlgorithm()", () => {
const algIdentifier = "hash" in webAlgorithm
? new AlgorithmIdentifier({ algorithm: asnAlgorithm })
: new AlgorithmIdentifier({
algorithm: id_ecPublicKey,
parameters: AsnConvert.serialize(new ECParameters({ namedCurve: asnAlgorithm })),
});
const result = ecAlgorithm.toWebAlgorithm(algIdentifier);
assert.ok(result);
assert.deepStrictEqual(result, webAlgorithm);
});
});
});
});
Loading
Loading