theophilusx / ssh2-sftp-client

a client for SSH2 SFTP
Apache License 2.0
797 stars 195 forks source link

Cannot parse privateKey: OpenSSH key integrity check failed -- bad passphrase? #534

Open harumugam-rythmos opened 1 month ago

harumugam-rythmos commented 1 month ago

@theophilusx this issue still exists, I haved tried in both 9.0.4 version and latest 10.0.3 version. Can you please check that?

Node version - 18.16.0

theophilusx commented 1 month ago

What type of key are you using?

Works fine for me using ssh-ed25519.

Also, see ssh2 issues log for problems using ssh-rsa and other pubkey authentication issues. The ssh2-sftp-client hands off ALL connection and authentication negotiation to ssh2, so if there is an issue with key authentication, it will be in that module. There is a lot of information in the issues log for the ssh2 repository you may find useful.

harumugam-rythmos commented 1 month ago

It works fine when the correct passphrase is used to decrypt the keyfile, but it is throwing this error if an incorrect password is provided which we cannot handle in catch block and it is getting caught in unhandledExceptions.

I see that the try/catch block enclosing the getConnection call is not catching the error.

await promiseRetry(retryOpts, async (retry, attempt) => {
        try {
          this.debugMsg(`connect: Connect attempt ${attempt}`);
          await this.getConnection(config);
        } catch (err) {
          switch (err.code) {
            case 'ENOTFOUND':
            case 'ECONNREFUSED':
            case 'ERR_SOCKET_BAD_PORT':
sftp.connect({
            host: sftpConfig.host,
            username: sftpConfig.username,
            password: sftpConfig.password,
            privateKey: sftpConfig.privateKey,
            passphrase: sftpConfig.passphrase,      // wrong passphrase
        });
theophilusx commented 1 month ago

What is the err.code for the uncaught exception?

How come you are sending a bad password AND a public key? If your server requires both a password and a public key, thyen you will need to write custom authentication Functions. See the ssh2 documentation.

harumugam-rythmos commented 1 month ago

err.code is undefined. We are just getting the error message like this Cannot parse privateKey: OpenSSH key integrity check failed -- bad passphrase?.

Our code does not need any public key, we are depending on private key and password.

theophilusx commented 1 month ago

Then the issue ins't the catch because the catch includes a case for undefined (which you did not include in the snippet).

When I refer to public key, I meant private key (it is often called public key because you have a public and private key combination, the server has your public key and you provide your private key). You said it fails when you provide wrong password - did you mean passphrase (i.e. the private key has a passphrase) or did you mean actual password (some badly configured servers can be setup to require both a private key AND a password in addition to the passphrase for the key)?

Exactly what error is caught by the unhandled exception handler?

I suspect the serer is emitting an error event, not an error exception. try/catch cannot work reliably with events because an event can be emitted after the block of code containing the catch has completed. However, there are global as well as connect listeners which should be catching such errors.

You really need to provide a complete log before I can tell exactly what is going oin as your explination is not detialed enough. A sample scirpt which reproduces the issue would also help and it would be good to confirm what versions of ssh2-sftp-client and node and on what platform you are running.

On Wed, 15 May 2024 at 20:27, Hariharan Arumugam @.***> wrote:

err.code is undefined. We are just getting the error message like this Cannot parse privateKey: OpenSSH key integrity check failed -- bad passphrase?.

Our code does not need any public key, we are depending on private key and password.

— Reply to this email directly, view it on GitHub https://github.com/theophilusx/ssh2-sftp-client/issues/534#issuecomment-2112141333, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMFIKLZPXMO4E3LWZFOOQTZCM2AHAVCNFSM6AAAAABHQNYEZ6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJSGE2DCMZTGM . You are receiving this because you were mentioned.Message ID: @.***>

-- regards,

Tim

-- Tim Cross