Skip to content

cryptography.exceptions.InternalError: Unknown OpenSSL error #1590

Closed

Description

@kylefoley76

I am receiving the following error message:

`
Unknown exception: Unknown OpenSSL error. This error is commonly encountered when another library is not cleaning up the OpenSSL error stack. If you are using cryptography with another library that uses OpenSSL try disabling it before reporting a bug. Otherwise please file an issue at https://github.com/pyca/cryptography/issues with information on how to reproduce this. ([_OpenSSLErrorWithText(code=50905205, lib=3, func=140, reason=117, reason_text=b'error:0308C075:bignum routines:BN_generate_dsa_nonce:private key too large'), _OpenSSLErrorWithText(code=168210435, lib=10, func=107, reason=3, reason_text=b'error:0A06B003:dsa routines:DSA_sign_setup:BN lib'), _OpenSSLErrorWithText(code=168230915, lib=10, func=112, reason=3, reason_text=b'error:0A070003:dsa routines:DSA_do_sign:BN lib')])
Traceback (most recent call last):
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/transport.py", line 2109, in run
handler(self.auth_handler, m)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/auth_handler.py", line 298, in _parse_service_accept
sig = self.private_key.sign_ssh_data(blob)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/dsskey.py", line 117, in sign_ssh_data
sig = key.sign(data, hashes.SHA1())
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/dsa.py", line 194, in sign
return _dsa_sig_sign(self._backend, self, data)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/dsa.py", line 29, in _dsa_sig_sign
backend.openssl_assert(res == 1)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 131, in openssl_assert
return binding._openssl_assert(self._lib, ok)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/bindings/openssl/binding.py", line 70, in _openssl_assert
raise InternalError(
cryptography.exceptions.InternalError: Unknown OpenSSL error. This error is commonly encountered when another library is not cleaning up the OpenSSL error stack. If you are using cryptography with another library that uses OpenSSL try disabling it before reporting a bug. Otherwise please file an issue at https://github.com/pyca/cryptography/issues with information on how to reproduce this. ([_OpenSSLErrorWithText(code=50905205, lib=3, func=140, reason=117, reason_text=b'error:0308C075:bignum routines:BN_generate_dsa_nonce:private key too large'), _OpenSSLErrorWithText(code=168210435, lib=10, func=107, reason=3, reason_text=b'error:0A06B003:dsa routines:DSA_sign_setup:BN lib'), _OpenSSLErrorWithText(code=168230915, lib=10, func=112, reason=3, reason_text=b'error:0A070003:dsa routines:DSA_do_sign:BN lib')])

Traceback (most recent call last):
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/client.py", line 680, in _auth
self._transport.auth_publickey(username, key)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/transport.py", line 1580, in auth_publickey
return self.auth_handler.wait_for_response(my_event)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/auth_handler.py", line 236, in wait_for_response
raise e
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/transport.py", line 2109, in run
handler(self.auth_handler, m)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/auth_handler.py", line 298, in _parse_service_accept
sig = self.private_key.sign_ssh_data(blob)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/dsskey.py", line 117, in sign_ssh_data
sig = key.sign(data, hashes.SHA1())
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/dsa.py", line 194, in sign
return _dsa_sig_sign(self._backend, self, data)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/dsa.py", line 29, in _dsa_sig_sign
backend.openssl_assert(res == 1)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 131, in openssl_assert
return binding._openssl_assert(self._lib, ok)
File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/bindings/openssl/binding.py", line 70, in _openssl_assert
raise InternalError(
cryptography.exceptions.InternalError: Unknown OpenSSL error. This error is commonly encountered when another library is not cleaning up the OpenSSL error stack. If you are using cryptography with another library that uses OpenSSL try disabling it before reporting a bug. Otherwise please file an issue at https://github.com/pyca/cryptography/issues with information on how to reproduce this. ([_OpenSSLErrorWithText(code=50905205, lib=3, func=140, reason=117, reason_text=b'error:0308C075:bignum routines:BN_generate_dsa_nonce:private key too large'), _OpenSSLErrorWithText(code=168210435, lib=10, func=107, reason=3, reason_text=b'error:0A06B003:dsa routines:DSA_sign_setup:BN lib'), _OpenSSLErrorWithText(code=168230915, lib=10, func=112, reason=3, reason_text=b'error:0A070003:dsa routines:DSA_do_sign:BN lib')])
`

I'm really at a loss here because I have no experience with SSH. I just want to SSH into a remote google cloud virtual instance and I want to do it with Paramiko because you can't log in with python with the google-api-client. In any case, the first error message I got was that when I put in the following code:

`
import paramiko
username = 'me'
hostname = '99.99.99.99'
client = paramiko.SSHClient()

key_filename = "/Users/kylefoley/.ssh/ssh_key"
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()
c = client.connect(username=username,
                   hostname=hostname,
                   key_filename=key_filename,
                   look_for_keys=False,
                   allow_agent=False)

`
(Ignore the identation errors.)
I was hit with the following error:

Traceback (most recent call last): File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/client.py", line 676, in _auth key = self._key_from_filepath( File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/client.py", line 586, in _key_from_filepath key = klass.from_private_key_file(key_path, password) File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/pkey.py", line 235, in from_private_key_file key = cls(filename=filename, password=password) File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/ed25519key.py", line 63, in __init__ signing_key = self._parse_signing_key_data(data, password) File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/ed25519key.py", line 96, in _parse_signing_key_data raise PasswordRequiredException( paramiko.ssh_exception.PasswordRequiredException: Private key file is encrypted

I then used Python's SSH-agent and I was able to get a password but
I'm not sure if I used it correctly. These were the commandlines I used

(venv) Admins-MacBook-Pro-4:byu_corpus kylefoley$ eval 'ssh-agent' SSH_AUTH_SOCK=/var/folders/vd/5ccxv4957f1_prjqt1l_ppsw0000gq/T//ssh-goky0gUep80V/agent.4526; export SSH_AUTH_SOCK; SSH_AGENT_PID=4527; export SSH_AGENT_PID; echo Agent pid 4527; (venv) Admins-MacBook-Pro-4:byu_corpus kylefoley$ ssh-add ~/.ssh/ssh_key Enter passphrase for /Users/kylefoley/.ssh/ssh_key: Bad passphrase, try again for /Users/kylefoley/.ssh/ssh_key: Bad passphrase, try again for /Users/kylefoley/.ssh/ssh_key: Identity added: /Users/kylefoley/.ssh/ssh_key (kfoley76)

I then added this line
password='my_password'
And the following parameter like so:
c = client.connect(username=username, hostname=hostname, key_filename=key_filename, look_for_keys=False, allow_agent=False, password=password)
I then got some errors in the def _check_dsa_parameters(parameters): function. I applied the patch recommended on this site but it did not work, so I just commented out that function. And that's when I hit the above error. Any suggestions would be appreciated.

Activity

  1. kylefoley76 commented on Jan 8, 2020

    @kylefoley76
    Author

    Also, I should add that I change my password to something wrong just to see what would happen and I did get the error: paramiko.ssh_exception.BadAuthenticationType: Bad authentication type which is what is supposed to happen. So it appears that the bug does not lie with the way I generated the password.

    Further, I don't understand what the following means:

    If you are using cryptography with another library that uses OpenSSL try disabling it before reporting a bug

  2. ploxiln commented on Jan 8, 2020

    @ploxiln
    Contributor

    To do a multi-line code-quote, surround it with triple-backticks on their own lines, like this:

    ```
    that was on its own line
        here is an indented line
    and another triple-backtick to finish
    ```
    

    You can edit your existing issue text and comments to make them more readable.

    The root problem seems to be this error reported from openssl:

    reason_text=b'error:0308C075:bignum routines:BN_generate_dsa_nonce:private key too large'

    It looks like the DSS key is on your side:

    File "/.../python3.8/site-packages/paramiko/auth_handler.py", line 298, in _parse_service_accept
      sig = self.private_key.sign_ssh_data(blob)
    File "/.../python3.8/site-packages/paramiko/dsskey.py", line 117, in sign_ssh_data
      sig = key.sign(data, hashes.SHA1())
    File "/.../python3.8/site-packages/cryptography/hazmat/backends/openssl/dsa.py", line 194, in sign
      return _dsa_sig_sign(self._backend, self, data)
    

    Something is weird about it - maybe it's not really a DSS key, or maybe support for DSS keys has sort of "bitrotted" because those are very rarely used anymore ...

  3. kylefoley76 commented on Jan 9, 2020

    @kylefoley76
    Author

    Please bear in mind that you're talking to someone who has almost no experience with SSH. Can you please dumb it down a little and give me some instructions. Thanks.

  4. ploxiln commented on Jan 9, 2020

    @ploxiln
    Contributor

    Don't apply "the patch" you saw in the other issue, that was for working-around an issue with old network appliances. (Those odd-sized DSS keys are very inadvisable, but if you just want to interoperate with the device and don't care about modern-internet-grade security, that's what that patch was for.) You're connecting to a more modern server, that is not the problem. I suggest uninstalling "cryptography" and then reinstalling it to undo any changes.

    If you successfully started ssh-agent and added your key, then try allow_agent=True and don't specify key_filename or password to client.connect().

    Can you generate and use a 2048-bit or bigger RSA key instead? Or an ed25519 key? What version of paramiko are you using? cryptography version? python version? OS version?

  5. kylefoley76 commented on Jan 9, 2020

    @kylefoley76
    Author

    Thanks for the quick response. Also thanks for the tip about the triple back ticks, that always irritated me in the past, now I can get around that issue.

    Cryptography Version (which I uninstalled and reinstalled): 2.8
    Python Version: 3.8
    Mac OS version: 10.14.4 Mojave
    Paramiko Version: 2.7.1

    I rewrote my code as:

    
        username = 'me'
        hostname = '99.99.99.99'
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.load_system_host_keys()
        c = client.connect(username=username,
                           hostname=hostname,
                           allow_agent=True,
                           )
        stdin, stdout, stderr = client.exec_command("cat /etc/os-release")  
    

    And I'm getting the error:

    Traceback (most recent call last):
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/client.py", line 700, in _auth
        self._transport.auth_publickey(username, key)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/transport.py", line 1580, in auth_publickey
        return self.auth_handler.wait_for_response(my_event)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/auth_handler.py", line 250, in wait_for_response
        raise e
    paramiko.ssh_exception.AuthenticationException: Authentication failed.
    

    But I don't see how this code can find out that I'm using an SSH agent and further get the information that the SSH agent has gotten. Also back when I was getting the error about the number of bytes of parameter.q I was only getting the number 17 whereas parameter.p was a very large number. When I performed the command line instructions for ssh-add I just added an 8 letter word as my password. Was I supposed to put it in the large 10 line or so string that is found on the SSH file?

  6. kylefoley76 commented on Jan 9, 2020

    @kylefoley76
    Author

    Also, I'm little worried that when I get to this line

    stdin, stdout, stderr = client.exec_command("cat /etc/os-release")  
    

    It will throw an error since that assumes Linux. I'm using a Mac to ssh into a Linux. Is that going to matter?

  7. ploxiln commented on Jan 9, 2020

    @ploxiln
    Contributor

    That line is fine, the command runs on the remote host, not the local one.

    macOS comes with openssh installed, which includes the commands ssh, ssh-add, ssh-agent, ssh-keygen and a few more. Your short script is pretty much equivalent to:

    ssh [email protected] cat /etc/os-release

    Both the openssh ssh command, and paramiko's client, by default will try to use the ssh-agent if they see the environment variable SSH_AUTH_SOCK. But you don't have to use the ssh-agent, that is optional, there are a few other ways to access keys. (I thought it looked like you had successfully loaded your key with ssh-add so that's why I suggested trying to just use the agent.)

    An ssh private key can be encrypted with a password, but this also is optional. The command ssh-add does not add a password to the private key - you have to specify the correct password if needed in order to decrypt the key so it can add the unprotected and usable private key to the ssh-agent. To add, change, or remove a password for a private key file (by encrypting or decrypting the private key) you can use the ssh-keygen command. Encrypted keys can also be used by the ssh client directly, without going through the agent, but you have to specify the password every time.

    SSH keys have public and private parts, and the public part needs to be already configured on the server-side in order for the private key to be granted access. Where did you get this key from?

    I suggest getting the plain ssh command to work first. If that doesn't work, paramiko won't work either, and paramiko isn't the simplest way ...

  8. ploxiln commented on Jan 9, 2020

    @ploxiln
    Contributor

    To specify a private key file directly to the ssh command, use the -i flag, like:

    ssh -i ~/.ssh/ssh_key [email protected] cat /etc/os-release

    (that's simpler than trying to use the ssh-agent)

  9. kylefoley76 commented on Jan 9, 2020

    @kylefoley76
    Author

    When I did

    ssh -i ~/.ssh/ssh_key [email protected] cat /etc/os-release
    

    Changing 'me' and the ip address of course, I got a permission denied (public key) error, same when I put sudo in front of it and same when I change ssh_key to ssh_key.pub. Also I performed the instructions yesterday

    (venv) Admins-MacBook-Pro-4:byu_corpus kylefoley$ eval 'ssh-agent'
    SSH_AUTH_SOCK=/var/folders/vd/5ccxv4957f1_prjqt1l_ppsw0000gq/T//ssh-goky0gUep80V/agent.4526; export SSH_AUTH_SOCK;
    SSH_AGENT_PID=4527; export SSH_AGENT_PID;
    echo Agent pid 4527;
    (venv) Admins-MacBook-Pro-4:byu_corpus kylefoley$ ssh-add ~/.ssh/ssh_key
    Enter passphrase for /Users/kylefoley/.ssh/ssh_key: 
    Identity added: /Users/kylefoley/.ssh/ssh_key (kfoley76)
    

    And did them again to no avail.

  10. ploxiln commented on Jan 9, 2020

    @ploxiln
    Contributor

    That means the key or the username is wrong, so the server rejects you.

  11. kylefoley76 commented on Jan 9, 2020

    @kylefoley76
    Author

    Well, I'm clearly able to ssh into that computer with the gcloud command line using the following

    gcloud init && gcloud compute ssh kfoley76 --project atomic-drake-250022 --verbosity=debug --zone=us-west2-a
    

    And then the following demonstrates that I'm using the correct external ip address:
    image
    And this is the exact command line I'm using:

    ssh -i ~/.ssh/ssh_key [email protected] cat /etc/os-release
    
  12. kylefoley76 commented on Jan 9, 2020

    @kylefoley76
    Author

    Maybe when I ssh into that computer I have to do chmod or something like that.

  13. ploxiln commented on Jan 9, 2020

    @ploxiln
    Contributor
  14. kylefoley76 commented on Jan 9, 2020

    @kylefoley76
    Author

    Ok, I'm making progress but still not there. I got the permissions error removed:

    (venv) Admins-MacBook-Pro-4:byu_corpus kylefoley$ eval 'ssh-agent' && ssh-add ~/.ssh/google_compute_engine
    SSH_AUTH_SOCK=/var/folders/vd/5ccxv4957f1_prjqt1l_ppsw0000gq/T//ssh-5gof3tR72jnx/agent.6385; export SSH_AUTH_SOCK;
    SSH_AGENT_PID=6386; export SSH_AGENT_PID;
    echo Agent pid 6386;
    Identity added: /Users/kylefoley/.ssh/google_compute_engine ([email protected])
    (venv) Admins-MacBook-Pro-4:byu_corpus kylefoley$ ssh -i ~/.ssh/ssh_key [email protected] cat /etc/os-release
    PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
    NAME="Debian GNU/Linux"
    VERSION_ID="9"
    VERSION="9 (stretch)"
    VERSION_CODENAME=stretch
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"
    

    But it is still the case that when I run:

    username = 'kfoley76'
        hostname = '34.94.16.180'
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.load_system_host_keys()
        c = client.connect(username=username,
                           hostname=hostname,
                           # key_filename=key_filename,
                           allow_agent=True,
                           )
    

    I get the error:

    Traceback (most recent call last):
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/client.py", line 700, in _auth
        self._transport.auth_publickey(username, key)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/transport.py", line 1580, in auth_publickey
        return self.auth_handler.wait_for_response(my_event)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/auth_handler.py", line 250, in wait_for_response
        raise e
    paramiko.ssh_exception.AuthenticationException: Authentication failed.
    

    And when I explicitly state keyfile like so:

        username = 'kfoley76'
        hostname = '34.94.16.180'
        client = paramiko.SSHClient()
        key_filename = "/Users/kylefoley/.ssh/google_compute_engine"
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.load_system_host_keys()
        c = client.connect(username=username,
                           hostname=hostname,
                           key_filename=key_filename,
                           allow_agent=True,
                           )
    

    I get the error:

    Unknown exception: q must be exactly 160, 224, or 256 bits long
    Traceback (most recent call last):
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/transport.py", line 2109, in run
        handler(self.auth_handler, m)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/auth_handler.py", line 298, in _parse_service_accept
        sig = self.private_key.sign_ssh_data(blob)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/dsskey.py", line 108, in sign_ssh_data
        key = dsa.DSAPrivateNumbers(
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 262, in private_key
        return backend.load_dsa_private_numbers(self)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 657, in load_dsa_private_numbers
        dsa._check_dsa_private_numbers(numbers)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 161, in _check_dsa_private_numbers
        _check_dsa_parameters(parameters)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 135, in _check_dsa_parameters
        raise ValueError("q must be exactly 160, 224, or 256 bits long")
    ValueError: q must be exactly 160, 224, or 256 bits long
    
    Traceback (most recent call last):
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/client.py", line 680, in _auth
        self._transport.auth_publickey(username, key)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/transport.py", line 1580, in auth_publickey
        return self.auth_handler.wait_for_response(my_event)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/auth_handler.py", line 236, in wait_for_response
        raise e
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/transport.py", line 2109, in run
        handler(self.auth_handler, m)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/auth_handler.py", line 298, in _parse_service_accept
        sig = self.private_key.sign_ssh_data(blob)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/dsskey.py", line 108, in sign_ssh_data
        key = dsa.DSAPrivateNumbers(
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 262, in private_key
        return backend.load_dsa_private_numbers(self)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 657, in load_dsa_private_numbers
        dsa._check_dsa_private_numbers(numbers)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 161, in _check_dsa_private_numbers
        _check_dsa_parameters(parameters)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 135, in _check_dsa_parameters
        raise ValueError("q must be exactly 160, 224, or 256 bits long")
    ValueError: q must be exactly 160, 224, or 256 bits long
    

    And here the value for q is 65537

  15. ploxiln commented on Jan 9, 2020

    @ploxiln
    Contributor

    quick note: eval 'ssh-agent' does not achieve anything. it should be:

    eval `ssh-agent`
    

    but it doesn't matter because macOS already runs an ssh-agent in the background for you by default (so that's why the agent has seemed to work thus far).

    You can use the command ssh-add -l to see the type of key you have loaded, this may be interesting.

    You should not have to specify the key if you loaded it into the ssh-agent and allow using the agent. It's good to know exactly which way is being used, while debugging. So try both individually to see if there is any difference, and then just pick one thereafter if not.

    In other cases of "unusual DSS/DSA keys", it was just p, the familiar bit-length, which was not one of the usual values. Different values for q don't really make sense ... it must be a mis-parsed key or just a very weird key somehow. (Which is why I wondered if going through the ssh-agent would work-around some kind of bug parsing the file directly.) What generated these key files anyway?

    I suggest making a new RSA key instead, and then get the gcloud command to set up the public part on the server side (the docs say "gcloud compute ssh ensures that the user's public SSH key is present in the project's metadata.")

    See also: https://security.stackexchange.com/questions/112802/why-openssh-deprecated-dsa-keys

  16. kylefoley76 commented on Jan 10, 2020

    @kylefoley76
    Author

    When I type ssh-add -l I get:

    2048 [secret string] kfoley76 (RSA)
    2048 [secret string] [email protected] (RSA)

    As far as what generated the ssh keys, that was

    ssh-keygen -t rsa -f ~/.ssh/ssh_key -C kfoley76

    When you say the following I do not know how to pick an ssh key.

    You should not have to specify the key if you loaded it into the ssh-agent and allow using the agent. It's good to know exactly which way is being used, while debugging. So try both individually to see if there is any difference, and then just pick one thereafter if not.

    I suggest making a new RSA key instead, and then get the gcloud command to set up the public part on the server side (the docs say "gcloud compute ssh ensures that the user's public SSH key is present in the project's metadata.")

    I'm not exactly sure what you mean by "get the gcloud command to set up the public part on the server side" but I did generate a new RSA key and SSH'ed into the vm instance again. I also checked the metadata of the vm instance where it says what SSH pub key is and its string matches the string found in the ssh_key.pub file. This was the syntax I used

        credentials = GoogleCredentials.get_application_default()
        service = discovery.build('compute', 'v1', credentials=credentials)
        project = 'atomic-drake-250022'  # TODO: Update placeholder value.
        zone = 'us-west2-a'  # TODO: Update placeholder value.
        instance = 'kfoley76'  # TODO: Update placeholder value.
    
        request1 = service.instances().get(project=project, zone=zone, instance=instance)
        response1 = request1.execute()
    	print(response1['metadata']['items'][0]['value'])
    

    I also added the new ssh_key to ssh agent and tried running the paramiko code again and got the same error. I'm wondering why when I typed

    ssh-keygen -t rsa -f ~/.ssh/ssh_key -C kfoley76
    

    It generated a new fingerprint the fingerprint it generated was different than the one found with

    response1['metadata']['fingerprint']
    

    Also only two new files where generated. Is this a problem? Should google_compute_engine also be created? Here is a screenshot of my .ssh folder:

    image

  17. ploxiln commented on Jan 10, 2020

    @ploxiln
    Contributor

    I mean, for client.connect(), either specify allow_agent=True with key_filename=None, or specify allow_agent=False with key_filename="/path/to/key", so when we get some error, we know for sure which it was trying to use.

    But anyway, I think I know what's going on. Your RSA key is being loaded as a DSS/DSA key. It's a long-standing historical issue with this paramiko client code, that when you specify a key file, it just tries loading it as all possible key types in turn, and uses whatever seems to work. In this case it seemed to load as a DSA key, but not work as a DSA key, because it's actually an RSA key.

    Try something like this:

        username = 'kfoley76'
        hostname = '34.94.16.180'
        pkey = paramiko.RSAKey(filename="/Users/kylefoley/.ssh/google_compute_engine")
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.load_system_host_keys()
        c = client.connect(username=username,
                           hostname=hostname,
                           pkey=pkey,
                           allow_agent=False,
                           )

    (but do also try with allow_agent=True and no key specified to client.connect())

    So I think there is a pretty bad bug in paramiko's support for new-format private keys which was added in paramiko-2.7.0, but we'll see if these last attempts confirm it.

  18. kylefoley76 commented on Jan 10, 2020

    @kylefoley76
    Author

    I'm getting the same error with both methods

    Traceback (most recent call last):
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/client.py", line 700, in _auth
        self._transport.auth_publickey(username, key)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/transport.py", line 1580, in auth_publickey
        return self.auth_handler.wait_for_response(my_event)
      File "/Users/kylefoley/codes/venv/lib/python3.8/site-packages/paramiko/auth_handler.py", line 250, in wait_for_response
        raise e
    paramiko.ssh_exception.AuthenticationException: Authentication failed.
    
  19. ploxiln commented on Jan 10, 2020

    @ploxiln
    Contributor

    ... but that's not the openssl error "BN_generate_dsa_nonce:private key too large" nor the cryptography error "q must be exactly 160, 224, or 256 bits long".

    What caused confusion at the beginning was trying to use the wrong key (along with paramiko's error handling in this area being bad and confusing). At this point you have two different keys loaded into the ssh-agent which macOS started for you by default (and probably 10 other ssh-agents you started later also hanging around but not really hurting).

    So I suggest clearing all keys from the ssh-agent with ssh-add -D and then not using ssh-add or ssh-agent anymore, just to simplify as much as possible. Just confirm that the ssh command with an explicit key file works, and then try to make that same explicit key file work with paramiko (by explicitly loading it as an RSAKey to work around the DSA key confusion in paramiko). The ssh command will ask you for a password to decrypt the key if needed, and if it is needed then you'll have to specify it to paramiko.RSAKey as well.

    If at that point the ssh command works but paramiko still doesn't, I'd just use ssh.

  20. kylefoley76 commented on Jan 10, 2020

    @kylefoley76
    Author

    I performed the command ssh-add -D, but then I was not exactly sure what you wanted me to do. So I performed:

        username = 'kfoley76'
        hostname = '34.94.16.180'
        pkey = paramiko.RSAKey(filename="/Users/kylefoley/.ssh/google_compute_engine")
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.load_system_host_keys()
        c = client.connect(username=username,
                           hostname=hostname,
                           pkey=pkey,
                           allow_agent=False,
                           )
    

    But that got the same error as last time. Also, if this is the end of the road as far as suggestions go, is this a confirmed bug that paramiko is working on and will be removed in the future? If so, when do you think it will be removed?

  21. ploxiln commented on Jan 10, 2020

    @ploxiln
    Contributor

    The request was to, after clearing the ssh-agent, do ssh -i /path/to/key user@host some-command to confirm again the exact key and password (or lack thereof) that is needed.

    The paramiko bug I suspect is: you just specify a key filename, and paramiko attempts to load your RSA key as a DSA key, and does not detect the error in the right way, causing all the strange DSA errors which you started with. But if you use the paramiko.RSAKey constructor like in the latest example, that avoids this suspected bug. If the explicit RSAKey constructor doesn't work, I have no idea what the issue is, and it won't be fixed.

  22. kylefoley76 commented on Jan 12, 2020

    @kylefoley76
    Author

    I'm getting permission denied errors.

    (venv) Admins-MacBook-Pro-4:pcode kylefoley$ ssh -i ~/.ssh/ssh_key [email protected] echo
    [email protected]: Permission denied (publickey).
    (venv) Admins-MacBook-Pro-4:pcode kylefoley$ ssh -i ~/.ssh/google_compute_engine [email protected]
    [email protected]: Permission denied (publickey).
    (venv) Admins-MacBook-Pro-4:pcode kylefoley$ ssh -i ~/.ssh/google_compute_engine [email protected] echo
    [email protected]: Permission denied (publickey).
    (venv) Admins-MacBook-Pro-4:pcode kylefoley$ open ~/.ssh
    (venv) Admins-MacBook-Pro-4:pcode kylefoley$ ssh -i ~/.ssh/ssh_key.pub [email protected]
    [email protected]: Permission denied (publickey).
    (venv) Admins-MacBook-Pro-4:pcode kylefoley$ ssh -i ~/.ssh/google_compute_engine.pub [email protected] echo
    [email protected]: Permission denied (publickey).
    

    But I was able to log in with gcloud. I looked at the info generated by the gcloud initilization and this is all of it except for the actual ssh key.

    DEBUG: Running [gcloud.compute.ssh] with arguments: [--project: "atomic-drake-250022", --verbosity: "debug", --zone: "us-west2-a", [USER@]INSTANCE: "kfoley76"]
    DEBUG: Current SSH keys in project: [u'kylefoley:ssh-rsa ... [email protected]', u'root:ssh-rsa ... [email protected]', u'kylefoley76:ssh-rsa ... [email protected]', u'kylefoley:ssh-rsa ... [email protected]']
    DEBUG: Running command [/usr/bin/ssh -t -i /Users/kylefoley/.ssh/google_compute_engine -o CheckHostIP=no -o HostKeyAlias=compute.7761185173670800145 -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/Users/kylefoley/.ssh/google_compute_known_hosts [email protected]].
    DEBUG: Executing command: [u'/usr/bin/ssh', u'-t', u'-i', u'/Users/kylefoley/.ssh/google_compute_engine', u'-o', u'CheckHostIP=no', u'-o', u'HostKeyAlias=compute.7761185173670800145', u'-o', u'IdentitiesOnly=yes', u'-o', u'StrictHostKeyChecking=yes', u'-o', u'UserKnownHostsFile=/Users/kylefoley/.ssh/google_compute_known_hosts', u'[email protected]']
    Linux kfoley76 4.9.0-11-amd64 #1 SMP Debian 4.9.189-3+deb9u2 (2019-11-11) x86_64
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    

    I also tried:

    (venv) Admins-MacBook-Pro-4:pcode kylefoley$ ssh -i ~/.ssh/google_compute_known_hosts [email protected] echo
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    Permissions 0644 for '/Users/kylefoley/.ssh/google_compute_known_hosts' are too open.
    It is required that your private key files are NOT accessible by others.
    This private key will be ignored.
    Load key "/Users/kylefoley/.ssh/google_compute_known_hosts": bad permissions
    [email protected]: Permission denied (publickey).
    
  23. ploxiln commented on Jan 12, 2020

    @ploxiln
    Contributor

    The gcloud command helper used the username kylefoley (and the ssh key /Users/kylefoley/.ssh/google_compute_engine, but the correct username is the new discovery).

    "known_hosts" is not a private key, it's something else. You don't have to do anything explicit with it right now, and hopefully not for a while, one topic at a time ...

  24. kylefoley76 commented on Jan 12, 2020

    @kylefoley76
    Author

    Ok, I was able to execute the program without throwing an error but I'm not given command line access to the new computer. I had to use [email protected] but I don't understand why since the name of my instance is clearly kfoley76 as seen here:

    image

    After I run the program and pwd I still get the same directory that I started from.

    Admins-MacBook-Pro-4:pcode kylefoley$ bbyu m2c ip
    3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) 
    [Clang 6.0 (clang-600.0.57)]
    3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) 
    [Clang 6.0 (clang-600.0.57)]
    PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
    NAME="Debian GNU/Linux"
    VERSION_ID="9"
    VERSION="9 (stretch)"
    VERSION_CODENAME=stretch
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"
    
    Admins-MacBook-Pro-4:pcode kylefoley$ pwd
    /Users/kylefoley/codes/pcode
    

    The command bbyu m2c ip runs the program:

        username = 'kylefoley'
        hostname = '34.94.16.180'
        pkey = paramiko.RSAKey(filename="/Users/kylefoley/.ssh/google_compute_engine")
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.load_system_host_keys()
        c = client.connect(username=username,
                           hostname=hostname,
                           pkey=pkey,
                           allow_agent=False,
                           )
    
        stdin, stdout, stderr = client.exec_command("cat /etc/os-release")  # assuming is linux
    
        print(stdout.read().decode())
    
  25. ploxiln commented on Jan 12, 2020

    @ploxiln
    Contributor

    After I run the program and pwd I still get the same directory that I started from.

    That is working as expected.

  26. kylefoley76 commented on Jan 13, 2020

    @kylefoley76
    Author

    I figured out how it works. Thanks very much for helping me with my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions