denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.64k stars 5.25k forks source link

ext/node: jwk public key export not implemented (PublicKeyObject) #18928

Closed Asguho closed 1 month ago

Asguho commented 1 year ago

Describe the bug

Using mineflayer from npm causes a "not implemented" error with the crypto module from the node compatibility layer

Steps to Reproduce

Running the code in this index.ts file

import mineflayer from 'npm:mineflayer'

const bot = mineflayer.createBot({
    username: 'use any value here',
    host: 'localhost',
    auth: 'microsoft'
})

bot.on('login', () => {
    console.log('Succesfully logged in!')
})

Results in this error

% deno run --allow-all index.ts
error: Uncaught Error: Not implemented: crypto.KeyObject.prototype.asymmetricKeyType
    at notImplemented (ext:deno_node/_utils.ts:7:11)
    at KeyObject.get asymmetricKeyType (ext:deno_node/internal/crypto/keys.ts:62:9)
    at keyToJWK (file:///C:/Users/aske/AppData/Local/deno/npm/registry.npmjs.org/jose/4.14.4/dist/node/cjs/runtime/key_to_jwk.js:35:85)
    at exportJWK (file:///C:/Users/aske/AppData/Local/deno/npm/registry.npmjs.org/jose/4.14.4/dist/node/cjs/key/export.js:16:40)
    at new XboxTokenManager (file:///C:/Users/aske/AppData/Local/deno/npm/registry.npmjs.org/prismarine-auth/2.2.0/src/TokenManagers/XboxTokenManager.js:19:5)
    at MicrosoftAuthFlow.initTokenManagers (file:///C:/Users/aske/AppData/Local/deno/npm/registry.npmjs.org/prismarine-auth/2.2.0/src/MicrosoftAuthFlow.js:72:16)
    at new MicrosoftAuthFlow (file:///C:/Users/aske/AppData/Local/deno/npm/registry.npmjs.org/prismarine-auth/2.2.0/src/MicrosoftAuthFlow.js:35:10)
    at Object.authenticate (file:///C:/Users/aske/AppData/Local/deno/npm/registry.npmjs.org/minecraft-protocol/1.41.2/src/client/microsoftAuth.js:21:43)
    at Object.createClient (file:///C:/Users/aske/AppData/Local/deno/npm/registry.npmjs.org/minecraft-protocol/1.41.2/src/createClient.js:50:25)
    at Object.createBot (file:///C:/Users/aske/AppData/Local/deno/npm/registry.npmjs.org/mineflayer/4.8.1/lib/loader.js:96:35)

Update (@kt3k): Now the error is:

error: Uncaught (in promise) Error: Not implemented: jwk public key export not implemented
    at notImplemented (ext:deno_node/_utils.ts:38:9)
    at PublicKeyObject.export (ext:deno_node/internal/crypto/keys.ts:714:7)
    at keyToJWK (file:///Users/kt3k/Library/Caches/deno/npm/registry.npmjs.org/jose/4.15.9/dist/node/cjs/runtime/key_to_jwk.js:38:32)
    ....

Expected behavior

% deno run --allow-all index.ts
Succesfully logged in!

Environment

OS: Windows 11 deno version: 1.31.1

hironichu commented 1 year ago

This is an ongoing implementation you can track here https://github.com/denoland/deno/issues/18455

levex commented 1 year ago

Some of this work will land in a PR in the coming days :-)

SgtPooki commented 1 year ago

@levex it's been a few months since your comment. Any updates here?

link-discord commented 1 year ago

Any updates? This is still an issue.

levex commented 1 year ago

Unfortunately, I no longer have time to spare on contributing to Deno - please raise this with the team. cc @bartlomieju you can probably assist in redirecting this ticket.

Asguho commented 11 months ago

Is there any news on redirecting this issue?

littledivy commented 11 months ago

KeyObject#asymmetricKeyType was added in #20981

We're tracking other node:crypto APIs in #18455 and private key APIs in #21124.

foobarfighter commented 10 months ago

@littledivy I'm still seeing this issue as of today.

[uncaught application error]: Error - connect: getConnection: Not implemented: crypto.KeyObject.prototype.asymmetricKeyType
    at SftpClient.fmtError (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2-sftp-client/9.1.0/src/index.js:82:22)
    at SftpClient.connect (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2-sftp-client/9.1.0/src/index.js:219:37)
    at eventLoopTick (ext:core/01_core.js:189:11)
    at async SFTPStorageClient.save (file:///Users/bob/workspace/bank-api/api/storage.ts:41:5)
    at async FileService.create (file:///Users/bob/workspace/bank-api/api/service.ts:37:5)
    at async file:///Users/bob/workspace/bank-api/api/main.ts:44:23
    at async dispatch (https://deno.land/x/oak@v12.6.1/middleware.ts:80:7)
    at async dispatch (https://deno.land/x/oak@v12.6.1/middleware.ts:80:7)
    at async dispatch (https://deno.land/x/oak@v12.6.1/middleware.ts:80:7)
    at async dispatch (https://deno.land/x/oak@v12.6.1/middleware.ts:80:7)
deno 1.38.1 (release, aarch64-apple-darwin)
v8 12.0.267.1
typescript 5.2.2
bartlomieju commented 10 months ago

@littledivy PTAL

foobarfighter commented 10 months ago

Here is some more information if it's helpful:

$ deno --version

deno 1.38.1 (release, aarch64-apple-darwin)
v8 12.0.267.1
typescript 5.2.2

Minimal test case. I'm trying to use the ssh2 npm module (in my previous comment I was using the ssh2-sftp-client npm module which uses ssh2 under the hood):

import { Client } from "npm:ssh2";

const client = new Client();
const p = client
  .on("ready", () => {
    console.log("ready");
  })
  .on("error", (e: Error) => {
    console.log(e);
  })
  .connect({ host: "127.0.0.1", port: 2022, username: "username", password: "*******" });

More informative stack trace:

Error: Not implemented: crypto.KeyObject.prototype.asymmetricKeyType
    at notImplemented (ext:deno_node/_utils.ts:9:9)
    at KeyObject.export (ext:deno_node/internal/crypto/keys.ts:79:5)
    at Curve25519Exchange.getPublicKey (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2/1.14.0/lib/protocol/kex.js:1268:46)
    at Curve25519Exchange.start (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2/1.14.0/lib/protocol/kex.js:996:29)
    at handleKexInit (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2/1.14.0/lib/protocol/kex.js:486:13)
    at Protocol.onKEXPayload (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2/1.14.0/lib/protocol/kex.js:1777:14)
    at NullDecipher.decrypt (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2/1.14.0/lib/protocol/crypto.js:612:26)
    at Protocol.parsePacket [as _parse] (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2/1.14.0/lib/protocol/Protocol.js:2021:25)
    at Protocol.parse (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2/1.14.0/lib/protocol/Protocol.js:306:16)
    at Socket.<anonymous> (file:///Users/bob/Library/Caches/deno/npm/registry.npmjs.org/ssh2/1.14.0/lib/client.js:775:21)
foobarfighter commented 10 months ago

I dug into this a little bit deeper. The error should reference crypto.KeyObject.prototype.export and not crypto.KeyObject.prototype.asymmetricKeyType. I believe there's a copy pasta bug there.

It also looks like this is unimplemented because in nodejs KeyObject.export is implemented in C++. How should node compatibility modules that have C++ bindings be implemented in deno? In this case, the nodejs C++ implementations have a dependency on openssl.

ansarizafar commented 7 months ago

I also have the same issue with Deno and libp2p. Is there any solution?

AndreaGiulianini commented 6 months ago

The same problem with packeage tunnel-ssh

sntran commented 5 months ago

Would like to see this to get ssh2 to work in Deno as well. It's the only SSH2/SFTP solution that we could use at the moment.

birkskyum commented 4 months ago

Hit a related:

ERROR Not implemented: crypto.PrivateKeyObject.prototype.export

in the

kt3k commented 2 months ago

Looks like now the error is: TypeError: exporting ec is not implemented

thrown in: https://github.com/denoland/deno/blob/e97764e7ecfea960987b63a550fb45bc3d2171da/ext/node/polyfills/internal/crypto/keys.ts#L416

birkskyum commented 1 month ago

Using the latest canary (deno 1.45.5+507e5b7) i now get

error: Uncaught (in promise) Error: Not implemented: jwk public key export not implemented

Can't tell if the exporting ec is not implemented issue is resolved, or if this jwk issue appear before it. @kt3k , can you tell?

kt3k commented 1 month ago

@birkskyum Thanks for checking! I confirmed the same error. Updated the title.

(I think the previous issue was resolved in https://github.com/denoland/deno/pull/24463)