2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-02-14 11:38:45 -05:00
|
|
|
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
|
|
|
|
|
2023-06-27 02:18:22 -04:00
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
2024-08-07 02:43:58 -04:00
|
|
|
import {
|
|
|
|
op_node_create_private_key,
|
|
|
|
op_node_create_public_key,
|
|
|
|
op_node_sign,
|
|
|
|
op_node_verify,
|
|
|
|
} from "ext:core/ops";
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-04-19 10:24:26 -04:00
|
|
|
import {
|
|
|
|
validateFunction,
|
|
|
|
validateString,
|
|
|
|
} from "ext:deno_node/internal/validators.mjs";
|
2023-07-02 14:19:30 -04:00
|
|
|
import { Buffer } from "node:buffer";
|
2023-03-08 06:44:54 -05:00
|
|
|
import type { WritableOptions } from "ext:deno_node/_stream.d.ts";
|
|
|
|
import Writable from "ext:deno_node/internal/streams/writable.mjs";
|
2023-02-14 11:38:45 -05:00
|
|
|
import type {
|
|
|
|
BinaryLike,
|
|
|
|
BinaryToTextEncoding,
|
|
|
|
Encoding,
|
|
|
|
PrivateKeyInput,
|
|
|
|
PublicKeyInput,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/crypto/types.ts";
|
2023-11-09 12:56:59 -05:00
|
|
|
import {
|
2024-08-07 02:43:58 -04:00
|
|
|
kConsumePrivate,
|
|
|
|
kConsumePublic,
|
2023-11-09 12:56:59 -05:00
|
|
|
KeyObject,
|
2023-12-02 23:28:13 -05:00
|
|
|
prepareAsymmetricKey,
|
2023-11-09 12:56:59 -05:00
|
|
|
} from "ext:deno_node/internal/crypto/keys.ts";
|
2024-08-07 02:43:58 -04:00
|
|
|
import { createHash } from "ext:deno_node/internal/crypto/hash.ts";
|
2023-04-19 10:24:26 -04:00
|
|
|
import { ERR_CRYPTO_SIGN_KEY_REQUIRED } from "ext:deno_node/internal/errors.ts";
|
2023-03-28 08:46:48 -04:00
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export type DSAEncoding = "der" | "ieee-p1363";
|
|
|
|
|
|
|
|
export interface SigningOptions {
|
|
|
|
padding?: number | undefined;
|
|
|
|
saltLength?: number | undefined;
|
|
|
|
dsaEncoding?: DSAEncoding | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface SignPrivateKeyInput extends PrivateKeyInput, SigningOptions {}
|
|
|
|
|
|
|
|
export interface SignKeyObjectInput extends SigningOptions {
|
|
|
|
key: KeyObject;
|
|
|
|
}
|
|
|
|
export interface VerifyPublicKeyInput extends PublicKeyInput, SigningOptions {}
|
|
|
|
|
|
|
|
export interface VerifyKeyObjectInput extends SigningOptions {
|
|
|
|
key: KeyObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type KeyLike = string | Buffer | KeyObject;
|
|
|
|
|
2023-04-19 10:24:26 -04:00
|
|
|
export class SignImpl extends Writable {
|
2023-03-28 08:46:48 -04:00
|
|
|
hash: Hash;
|
|
|
|
#digestType: string;
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
constructor(algorithm: string, _options?: WritableOptions) {
|
|
|
|
validateString(algorithm, "algorithm");
|
|
|
|
|
2023-03-28 08:46:48 -04:00
|
|
|
super({
|
|
|
|
write(chunk, enc, callback) {
|
|
|
|
this.update(chunk, enc);
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
algorithm = algorithm.toLowerCase();
|
|
|
|
|
|
|
|
this.#digestType = algorithm;
|
|
|
|
this.hash = createHash(this.#digestType);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sign(
|
2024-08-07 02:43:58 -04:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
privateKey: any,
|
2023-03-28 08:46:48 -04:00
|
|
|
encoding?: BinaryToTextEncoding,
|
2023-02-14 11:38:45 -05:00
|
|
|
): Buffer | string {
|
2024-08-07 02:43:58 -04:00
|
|
|
const res = prepareAsymmetricKey(privateKey, kConsumePrivate);
|
|
|
|
let handle;
|
|
|
|
if ("handle" in res) {
|
|
|
|
handle = res.handle;
|
|
|
|
} else {
|
|
|
|
handle = op_node_create_private_key(
|
|
|
|
res.data,
|
|
|
|
res.format,
|
|
|
|
res.type ?? "",
|
|
|
|
res.passphrase,
|
|
|
|
);
|
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
const ret = Buffer.from(op_node_sign(
|
2024-08-07 02:43:58 -04:00
|
|
|
handle,
|
2023-03-28 08:46:48 -04:00
|
|
|
this.hash.digest(),
|
|
|
|
this.#digestType,
|
|
|
|
));
|
|
|
|
return encoding ? ret.toString(encoding) : ret;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-03-28 08:46:48 -04:00
|
|
|
update(
|
|
|
|
data: BinaryLike | string,
|
|
|
|
encoding?: Encoding,
|
|
|
|
): this {
|
|
|
|
this.hash.update(data, encoding);
|
|
|
|
return this;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 10:24:26 -04:00
|
|
|
export function Sign(algorithm: string, options?: WritableOptions) {
|
|
|
|
return new SignImpl(algorithm, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sign.prototype = SignImpl.prototype;
|
|
|
|
|
|
|
|
export class VerifyImpl extends Writable {
|
2023-04-18 08:04:51 -04:00
|
|
|
hash: Hash;
|
|
|
|
#digestType: string;
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
constructor(algorithm: string, _options?: WritableOptions) {
|
|
|
|
validateString(algorithm, "algorithm");
|
|
|
|
|
2023-04-18 08:04:51 -04:00
|
|
|
super({
|
|
|
|
write(chunk, enc, callback) {
|
|
|
|
this.update(chunk, enc);
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
algorithm = algorithm.toLowerCase();
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-04-18 08:04:51 -04:00
|
|
|
this.#digestType = algorithm;
|
|
|
|
this.hash = createHash(this.#digestType);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:04:51 -04:00
|
|
|
update(data: BinaryLike, encoding?: string): this {
|
|
|
|
this.hash.update(data, encoding);
|
|
|
|
return this;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
verify(
|
2024-08-07 02:43:58 -04:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
publicKey: any,
|
2023-04-18 08:04:51 -04:00
|
|
|
signature: BinaryLike,
|
|
|
|
encoding?: BinaryToTextEncoding,
|
2023-02-14 11:38:45 -05:00
|
|
|
): boolean {
|
2024-08-07 02:43:58 -04:00
|
|
|
const res = prepareAsymmetricKey(publicKey, kConsumePublic);
|
|
|
|
let handle;
|
|
|
|
if ("handle" in res) {
|
|
|
|
handle = res.handle;
|
2023-04-18 08:04:51 -04:00
|
|
|
} else {
|
2024-08-07 02:43:58 -04:00
|
|
|
handle = op_node_create_public_key(
|
|
|
|
res.data,
|
|
|
|
res.format,
|
|
|
|
res.type ?? "",
|
|
|
|
res.passphrase,
|
2023-04-18 08:04:51 -04:00
|
|
|
);
|
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_node_verify(
|
2024-08-07 02:43:58 -04:00
|
|
|
handle,
|
2023-04-18 08:04:51 -04:00
|
|
|
this.hash.digest(),
|
|
|
|
this.#digestType,
|
|
|
|
Buffer.from(signature, encoding),
|
|
|
|
);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 10:24:26 -04:00
|
|
|
export function Verify(algorithm: string, options?: WritableOptions) {
|
|
|
|
return new VerifyImpl(algorithm, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Verify.prototype = VerifyImpl.prototype;
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export function signOneShot(
|
|
|
|
algorithm: string | null | undefined,
|
|
|
|
data: ArrayBufferView,
|
|
|
|
key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
|
2023-04-19 10:24:26 -04:00
|
|
|
callback?: (error: Error | null, data: Buffer) => void,
|
2023-02-14 11:38:45 -05:00
|
|
|
): Buffer | void {
|
2023-04-19 10:24:26 -04:00
|
|
|
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;
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function verifyOneShot(
|
|
|
|
algorithm: string | null | undefined,
|
2023-04-19 10:24:26 -04:00
|
|
|
data: BinaryLike,
|
2023-02-14 11:38:45 -05:00
|
|
|
key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
|
2023-04-19 10:24:26 -04:00
|
|
|
signature: BinaryLike,
|
|
|
|
callback?: (error: Error | null, result: boolean) => void,
|
2023-02-14 11:38:45 -05:00
|
|
|
): boolean | void {
|
2023-04-19 10:24:26 -04:00
|
|
|
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;
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
signOneShot,
|
|
|
|
verifyOneShot,
|
|
|
|
Sign,
|
|
|
|
Verify,
|
|
|
|
};
|