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

fix(ext/node): add crypto.sign|verify methods #18765

Merged
merged 3 commits into from
Apr 19, 2023
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
34 changes: 26 additions & 8 deletions cli/tests/unit_node/crypto_sign_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
assert,
assertEquals,
} from "../../../test_util/std/testing/asserts.ts";
import { createSign, createVerify } from "node:crypto";
import { createSign, createVerify, sign, verify } from "node:crypto";
import { Buffer } from "node:buffer";

const rsaPrivatePem = Buffer.from(
Expand Down Expand Up @@ -41,32 +41,50 @@ const table = [
},
];

const data = Buffer.from("some data to sign");

Deno.test({
name: "crypto.Sign - RSA PEM with SHA224, SHA256, SHA384, SHA512 digests",
name:
"crypto.Sign|sign - RSA PEM with SHA224, SHA256, SHA384, SHA512 digests",
fn() {
for (const testCase of table) {
for (const algorithm of testCase.algorithms) {
const signature = createSign(algorithm)
.update("some data to sign")
.sign(rsaPrivatePem, "hex");
assertEquals(signature, testCase.signature);
assertEquals(
createSign(algorithm)
.update(data)
.sign(rsaPrivatePem, "hex"),
testCase.signature,
);
assertEquals(
sign(algorithm, data, rsaPrivatePem),
Buffer.from(testCase.signature, "hex"),
);
}
}
},
});

Deno.test({
name: "crypto.Verify - RSA PEM with SHA224, SHA256, SHA384, SHA512 digests",
name:
"crypto.Verify|verify - RSA PEM with SHA224, SHA256, SHA384, SHA512 digests",
fn() {
for (const testCase of table) {
for (const algorithm of testCase.algorithms) {
assert(
createVerify(algorithm).update("some data to sign").verify(
createVerify(algorithm).update(data).verify(
rsaPublicPem,
testCase.signature,
"hex",
),
);
assert(
verify(
algorithm,
data,
rsaPublicPem,
Buffer.from(testCase.signature, "hex"),
),
);
}
}
},
Expand Down
94 changes: 61 additions & 33 deletions ext/node/polyfills/internal/crypto/sig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.

import { notImplemented } from "ext:deno_node/_utils.ts";
import { validateString } from "ext:deno_node/internal/validators.mjs";
import {
validateFunction,
validateString,
} from "ext:deno_node/internal/validators.mjs";
import { Buffer } from "ext:deno_node/buffer.ts";
import type { WritableOptions } from "ext:deno_node/_stream.d.ts";
import Writable from "ext:deno_node/internal/streams/writable.mjs";
Expand All @@ -17,6 +20,7 @@ import { KeyObject } from "ext:deno_node/internal/crypto/keys.ts";
import { createHash, Hash } from "ext:deno_node/internal/crypto/hash.ts";
import { KeyFormat, KeyType } from "ext:deno_node/internal/crypto/types.ts";
import { isArrayBufferView } from "ext:deno_node/internal/util/types.ts";
import { ERR_CRYPTO_SIGN_KEY_REQUIRED } from "ext:deno_node/internal/errors.ts";

const { core } = globalThis.__bootstrap;
const { ops } = core;
Expand All @@ -42,7 +46,7 @@ export interface VerifyKeyObjectInput extends SigningOptions {

export type KeyLike = string | Buffer | KeyObject;

export class Sign extends Writable {
export class SignImpl extends Writable {
hash: Hash;
#digestType: string;

Expand Down Expand Up @@ -103,7 +107,13 @@ export class Sign extends Writable {
}
}

export class Verify extends Writable {
export function Sign(algorithm: string, options?: WritableOptions) {
return new SignImpl(algorithm, options);
}

Sign.prototype = SignImpl.prototype;

export class VerifyImpl extends Writable {
hash: Hash;
#digestType: string;

Expand Down Expand Up @@ -165,47 +175,65 @@ export class Verify extends Writable {
}
}

export function Verify(algorithm: string, options?: WritableOptions) {
return new VerifyImpl(algorithm, options);
}

Verify.prototype = VerifyImpl.prototype;

export function signOneShot(
algorithm: string | null | undefined,
data: ArrayBufferView,
key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
): Buffer;
export function signOneShot(
algorithm: string | null | undefined,
data: ArrayBufferView,
key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
callback: (error: Error | null, data: Buffer) => void,
): void;
export function signOneShot(
_algorithm: string | null | undefined,
_data: ArrayBufferView,
_key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
_callback?: (error: Error | null, data: Buffer) => void,
callback?: (error: Error | null, data: Buffer) => void,
): Buffer | void {
notImplemented("crypto.sign");
if (algorithm != null) {
validateString(algorithm, "algorithm");
}

if (callback !== undefined) {
validateFunction(callback, "callback");
}

if (!key) {
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
}

const result = Sign(algorithm!).update(data).sign(key);

if (callback) {
setTimeout(() => callback(null, result));
} else {
return result;
}
}

export function verifyOneShot(
algorithm: string | null | undefined,
data: ArrayBufferView,
data: BinaryLike,
key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
signature: ArrayBufferView,
): boolean;
export function verifyOneShot(
algorithm: string | null | undefined,
data: ArrayBufferView,
key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
signature: ArrayBufferView,
callback: (error: Error | null, result: boolean) => void,
): void;
export function verifyOneShot(
_algorithm: string | null | undefined,
_data: ArrayBufferView,
_key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
_signature: ArrayBufferView,
_callback?: (error: Error | null, result: boolean) => void,
signature: BinaryLike,
callback?: (error: Error | null, result: boolean) => void,
): boolean | void {
notImplemented("crypto.verify");
if (algorithm != null) {
validateString(algorithm, "algorithm");
}

if (callback !== undefined) {
validateFunction(callback, "callback");
}

if (!key) {
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
}

const result = Verify(algorithm!).update(data).verify(key, signature);

if (callback) {
setTimeout(() => callback(null, result));
} else {
return result;
}
}

export default {
Expand Down