diff options
Diffstat (limited to 'ext/node/polyfills/internal/crypto/keys.ts')
-rw-r--r-- | ext/node/polyfills/internal/crypto/keys.ts | 75 |
1 files changed, 59 insertions, 16 deletions
diff --git a/ext/node/polyfills/internal/crypto/keys.ts b/ext/node/polyfills/internal/crypto/keys.ts index be85b44a3..e0c44cbf9 100644 --- a/ext/node/polyfills/internal/crypto/keys.ts +++ b/ext/node/polyfills/internal/crypto/keys.ts @@ -8,6 +8,7 @@ import { kHandle, kKeyObject, } from "ext:deno_node/internal/crypto/constants.ts"; +import { isStringOrBuffer } from "ext:deno_node/internal/crypto/cipher.ts"; import { ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE, ERR_INVALID_ARG_TYPE, @@ -16,7 +17,6 @@ import { import { notImplemented } from "ext:deno_node/_utils.ts"; import type { KeyFormat, - KeyType, PrivateKeyInput, PublicKeyInput, } from "ext:deno_node/internal/crypto/types.ts"; @@ -39,6 +39,9 @@ import { forgivingBase64UrlEncode as encodeToBase64Url, } from "ext:deno_web/00_infra.js"; +const { core } = globalThis.__bootstrap; +const { ops } = core; + export const getArrayBufferOrView = hideStackFrames( ( buffer, @@ -168,18 +171,6 @@ export class KeyObject { return this[kKeyType]; } - get asymmetricKeyDetails(): AsymmetricKeyDetails | undefined { - notImplemented("crypto.KeyObject.prototype.asymmetricKeyDetails"); - - return undefined; - } - - get asymmetricKeyType(): KeyType | undefined { - notImplemented("crypto.KeyObject.prototype.asymmetricKeyType"); - - return undefined; - } - get symmetricKeySize(): number | undefined { notImplemented("crypto.KeyObject.prototype.symmetricKeySize"); @@ -219,10 +210,33 @@ export interface JsonWebKeyInput { format: "jwk"; } +function prepareAsymmetricKey(key) { + if (isStringOrBuffer(key)) { + return { format: "pem", data: getArrayBufferOrView(key, "key") }; + } else if (typeof key == "object") { + const { key: data, encoding, format, type } = key; + if (!isStringOrBuffer(data)) { + throw new TypeError("Invalid key type"); + } + + return { + data: getArrayBufferOrView(data, "key", encoding), + format: format ?? "pem", + encoding, + type, + }; + } + + throw new TypeError("Invalid key type"); +} + export function createPrivateKey( - _key: PrivateKeyInput | string | Buffer | JsonWebKeyInput, -): KeyObject { - notImplemented("crypto.createPrivateKey"); + key: PrivateKeyInput | string | Buffer | JsonWebKeyInput, +): PrivateKeyObject { + const { data, format, type } = prepareAsymmetricKey(key); + const details = ops.op_node_create_private_key(data, format, type); + const handle = setOwnedKey(copyBuffer(data)); + return new PrivateKeyObject(handle, details); } export function createPublicKey( @@ -316,6 +330,35 @@ export class SecretKeyObject extends KeyObject { } } +const kAsymmetricKeyType = Symbol("kAsymmetricKeyType"); +const kAsymmetricKeyDetails = Symbol("kAsymmetricKeyDetails"); + +class AsymmetricKeyObject extends KeyObject { + constructor(type: KeyObjectType, handle: unknown, details: unknown) { + super(type, handle); + this[kAsymmetricKeyType] = details.type; + this[kAsymmetricKeyDetails] = { ...details }; + } + + get asymmetricKeyType() { + return this[kAsymmetricKeyType]; + } + + get asymmetricKeyDetails() { + return this[kAsymmetricKeyDetails]; + } +} + +class PrivateKeyObject extends AsymmetricKeyObject { + constructor(handle: unknown, details: unknown) { + super("private", handle, details); + } + + export(_options: unknown) { + notImplemented("crypto.PrivateKeyObject.prototype.export"); + } +} + export function setOwnedKey(key: Uint8Array): unknown { const handle = {}; KEY_STORE.set(handle, key); |