Skip to content

Fixes #325 -- add support for Ed25519 keys - #972

Merged
bitprophet merged 14 commits into
paramiko:masterfrom
alex:ed25519-support
Jun 6, 2017
Merged

bitprophet merged 14 commits into
paramiko:masterfrom
alex:ed25519-support

Conversation

@alex

@alex alex commented May 27, 2017 •

Copy link
Copy Markdown
Member

@alex

alex commented May 27, 2017

Copy link
Copy Markdown
Member Author

I haven't done any integration testing, besides the tests themselves. Can you suggest where and how to add more tests for this?

@ploxiln

ploxiln commented May 27, 2017

Copy link
Copy Markdown
Contributor

looks like a unit test could go in tests/test_pkey.py and an integration test could go in tests/test_client.py

Comment thread paramiko/client.py Outdated
keyfiles.append((keytype, full_path))

# look in ~/ssh/ for windows users:
full_path = os.path.expanduser("~/ssh/id_%s" % path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could parameterize a bit more e.g.

for ssh_dir in ["~/.ssh", "~/ssh"]:
    full_path = os.path.expanduser("%s/id_%s" % (ssh_dir, path))
    ...

@mithrandi mithrandi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file format stuff all looks correct to me.

Comment thread paramiko/ed25519key.py Outdated
def unpad(data):
# At the moment, this is only used for unpadding private keys on disk, and
# only unencrypted ones at that. In the future, if either of those changes,
# this really ought to be made constant time.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this comment is accurate, unpad seems to be used for encrypted keys too. Although, I'm not sure how anyone able to observe the timing of this code would not have access to the ciphertext as well, which would appear to render a timing attack irrelevant.

Comment thread paramiko/ed25519key.py
from paramiko.ssh_exception import SSHException, PasswordRequiredException


OPENSSH_AUTH_MAGIC = b"openssh-key-v1\x00"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: You seem to flipflop between 'strings' and "strings" in this code, maybe stick to one? 😼

@ploxiln

ploxiln commented May 27, 2017

Copy link
Copy Markdown
Contributor

very nice 👍

@bitprophet

Copy link
Copy Markdown
Member

OMG, I love you @alex! Will take a crack at testing & merging this early next week.

Comment thread setup.py Outdated
install_requires=[
'bcrypt>=3.0.0',
'cryptography>=1.1',
'pynacl',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would love at least some pinning here; latest pynacl seems to be 1.1.2. What's good - pynacl>=1.0, >=1.1, other?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reaperhulk opinions on an appropriate minimum version?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=1.0 (or 1.0.1) would be fine. That's the cffi 1.0 out-of-line version.

@bitprophet

Copy link
Copy Markdown
Member

Testing this out...

  • Localhost:
    • OS X Sierra 10.12.5
    • pyenv-compiled Python 2.7.12
    • stock OS X OpenSSH ssh 7.4p1, LibreSSL 2.5.0 (...huh.)
  • Target:
    • Debian 8
    • stock OpenSSH sshd (6.7p1)
    • which appears to have 3x host keys installed and enabled (in order: ED25519, RSA, ECDSA)

Sanity check with ssh, Fabric 2 and Paramiko master (massaging my known_hosts in between, as necessary, to avoid complaints):

  • With all 3 host keys enabled:
    • ssh/sshd agree on the ED25519 key, everything works great
    • Paramiko/sshd agree on the RSA key, everything works great
  • With the RSA and ECDSA keys disabled, leaving only the ED25519 key:
    • ssh/sshd still happy as clams
    • Paramiko, as expected, goes kaput because no host key could be agreed upon.

Unfortunately, when I switch from Paramiko master to an integration branch based on this PR (still with only the ED25519 key enabled on the target) Paramiko dies with Signature verification (ssh-ed25519) failed:

starting thread (client mode): 0x7ab8990L
Local version/idstring: SSH-2.0-paramiko_2.1.2
Remote version/idstring: SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u1
Connected (version 2.0, client OpenSSH_6.7p1)
kex algos:[u'[email protected]', u'ecdh-sha2-nistp521', u'ecdh-sha2-nistp384', u'ecdh-sha2-nistp256', u'diffie-hellman-group-exchange-sha256'] server key:[u'ssh-ed25519'] client encrypt:[u'[email protected]', u'[email protected]', u'[email protected]', u'aes256-ctr', u'aes192-ctr', u'aes128-ctr'] server encrypt:[u'[email protected]', u'[email protected]', u'[email protected]', u'aes256-ctr', u'aes192-ctr', u'aes128-ctr'] client mac:[u'[email protected]', u'[email protected]', u'[email protected]', u'hmac-sha2-512', u'hmac-sha2-256', u'[email protected]'] server mac:[u'[email protected]', u'[email protected]', u'[email protected]', u'hmac-sha2-512', u'hmac-sha2-256', u'[email protected]'] client compress:[u'none', u'[email protected]'] server compress:[u'none', u'[email protected]'] client lang:[u''] server lang:[u''] kex follows?False
Kex agreed: diffie-hellman-group-exchange-sha256
Cipher agreed: aes128-ctr
MAC agreed: hmac-sha2-256
Compression agreed: none
Got server p (2048 bits)
Exception: Signature verification (ssh-ed25519) failed.
Traceback (most recent call last):
  File "/Users/jforcier/Code/oss/paramiko/paramiko/transport.py", line 1851, in run
    self.kex_engine.parse_next(ptype, m)
  File "/Users/jforcier/Code/oss/paramiko/paramiko/kex_gex.py", line 91, in parse_next
    return self._parse_kexdh_gex_reply(m)
  File "/Users/jforcier/Code/oss/paramiko/paramiko/kex_gex.py", line 260, in _parse_kexdh_gex_reply
    self.transport._verify_key(host_key, sig)
  File "/Users/jforcier/Code/oss/paramiko/paramiko/transport.py", line 1697, in _verify_key
    raise SSHException('Signature verification (%s) failed.' % self.host_key_type) # noqa
SSHException: Signature verification (ssh-ed25519) failed.

When I dig, it's due to a BadSignatureException, whose traceback is thusly:

Traceback (most recent call last):
  File "/Users/jforcier/Code/oss/paramiko/paramiko/transport.py", line 1851, in run
    self.kex_engine.parse_next(ptype, m)
  File "/Users/jforcier/Code/oss/paramiko/paramiko/kex_gex.py", line 91, in parse_next
    return self._parse_kexdh_gex_reply(m)
  File "/Users/jforcier/Code/oss/paramiko/paramiko/kex_gex.py", line 260, in _parse_kexdh_gex_reply
    self.transport._verify_key(host_key, sig)
  File "/Users/jforcier/Code/oss/paramiko/paramiko/transport.py", line 1696, in _verify_key
    if not key.verify_ssh_sig(self.H, Message(sig)):
  File "/Users/jforcier/Code/oss/paramiko/paramiko/ed25519key.py", line 190, in verify_ssh_sig
    self._verifying_key.verify(data, msg.get_binary())
  File "/Users/jforcier/.virtualenvs/fabric2/lib/python2.7/site-packages/nacl/signing.py", line 115, in verify
    return nacl.bindings.crypto_sign_open(smessage, self._key)
  File "/Users/jforcier/.virtualenvs/fabric2/lib/python2.7/site-packages/nacl/bindings/crypto_sign.py", line 109, in crypto_sign_open
    raise exc.BadSignatureError("Signature was forged or corrupt")

I haven't looked any deeper yet; key exchange stuff isn't something I've had to muck with much before. Left to my own devices I'll dive in and educate myself and see if something obvious jumps out, otherwise, direction is welcome.

@alex

alex commented Jun 3, 2017

Copy link
Copy Markdown
Member Author

Grumble, this is what I get for not doing the integration testing myself. Will try to debug this weekend.

@alex

alex commented Jun 3, 2017

Copy link
Copy Markdown
Member Author

@bitprophet Ok, should be good now.

@alex
alex force-pushed the ed25519-support branch from 71b1d93 to 0ddb28f Compare June 3, 2017 06:03
@bitprophet

Copy link
Copy Markdown
Member

Thanks! Glad it wasn't me doing something silly. Confirmed my test case works once I get the recent commits

@alex

alex commented Jun 6, 2017

Copy link
Copy Markdown
Member Author

Nope, I was the one doing something silly.

@bitprophet
bitprophet merged commit 0ddb28f into paramiko:master Jun 6, 2017
bitprophet added a commit that referenced this pull request Jun 6, 2017
@alex
alex deleted the ed25519-support branch June 6, 2017 01:07
@zhangysh1995

Copy link
Copy Markdown

In version 2.2.1, I still have to manually pip3 install pynacl

The error:

root@194c224d0b6a:~/scripts# python3 tools/ssh.py 
Traceback (most recent call last):
  File "tools/ssh.py", line 2, in <module>
    from paramiko import SSHClient
  File "/usr/local/lib/python3.4/dist-packages/paramiko/__init__.py", line 31, in <module>
    from paramiko.transport import SecurityOptions, Transport
  File "/usr/local/lib/python3.4/dist-packages/paramiko/transport.py", line 57, in <module>
    from paramiko.ed25519key import Ed25519Key
  File "/usr/local/lib/python3.4/dist-packages/paramiko/ed25519key.py", line 22, in <module>
    import nacl.signing
ImportError: No module named 'nacl'

Openssl information:

root@194c224d0b6a:~/scripts# sudo apt show openssl   
Package: openssl
Priority: standard
Section: utils
Installed-Size: 929 kB
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Debian OpenSSL Team <[email protected]>
Version: 1.0.1f-1ubuntu2.22
Depends: libc6 (>= 2.15), libssl1.0.0 (>= 1.0.1)
Suggests: ca-certificates
Download-Size: 489 kB
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Supported: 5y
Task: standard, kubuntu-active, kubuntu-active, mythbuntu-frontend, mythbuntu-frontend, mythbuntu-desktop, mythbuntu-backend-slave, mythbuntu-backend-slave, mythbuntu-backend-master, mythbuntu-backend-master
APT-Manual-Installed: yes
APT-Sources: http://mirrors.163.com/ubuntu/ trusty-security/main amd64 Packages
Description: Secure Sockets Layer toolkit - cryptographic utility

N: There is 1 additional record. Please use the '-a' switch to see it

@ploxiln

ploxiln commented Jul 28, 2017

Copy link
Copy Markdown
Contributor

"pynacl" is an explicit requirement in setup.py - it is up to the method you used to install paramiko 2.2.1 to install pynacl at the same time. If you didn't use pip, you should probably use pip.

@vaygr

vaygr commented Oct 21, 2017

Copy link
Copy Markdown

Another day I was wondering if there's a way to reuse cryptography built-in support for this instead of introducing a train of libsodium dependencies, unless I'm missing something very obvious here.

Can't paramiko (and thus, something else, like Ansible) rely on the one crypto library and/or its backend, or at least have another one as optional for specific key support?

@alex

alex commented Oct 21, 2017 via email

Copy link
Copy Markdown
Member Author

@vaygr

vaygr commented Oct 21, 2017

Copy link
Copy Markdown

Ah, I see, it's my misunderstanding then.

So it's more like a temporary workaround than a solid permanent solution, waiting until the unified part "is ready".

Looking forward to it!

toabctl added a commit to toabctl/paramiko that referenced this pull request Aug 26, 2026
Ed25519 support arrived in 2.2 (paramiko#325 via paramiko#972) backed by PyNaCl,
because Cryptography had no Ed25519 primitive at the time; it landed in
Cryptography 2.6, roughly two years later. Our floor is already 3.3, so
the primitive has been available across the entire supported range, and
Ed25519Key was the last remaining user of PyNaCl.

Beyond dropping a C-extension dependency, this puts Ed25519 under the
same OpenSSL configuration that already governs every other key type,
cipher and KEX we use -- including any algorithm policy the host
OpenSSL enforces, which a separately bundled libsodium cannot
participate in. Compare KexCurve25519.is_available(), which already
defers to Cryptography for exactly that reason.

Implementation notes:

* Uses public_bytes(Encoding.Raw, PublicFormat.Raw) rather than the
  tidier public_bytes_raw(), which only exists as of Cryptography 40.
  The older spelling works across our whole supported range, including
  the 3.3.2 and 39.0.0 cells in CI.

* Ed25519Key._fields now holds the key's raw bytes rather than the key
  object itself. Cryptography's key objects compare by identity on
  older versions (3.3.2, 39.0.0) and are unhashable on newer ones, so
  neither is safe for the equality and hashing contract PKey relies on
  here; raw bytes are exact on every version. test_ed25519_compare
  gains a hash() assertion so it covers both failure modes.

* Cryptography's verify() takes (signature, data), the reverse of
  PyNaCl's verify(smessage, signature).

Wire format is unchanged. Ed25519 is deterministic, so the new
test_sign_and_verify_ed25519 pins the exact signature bytes the PyNaCl
implementation produced for the existing test key.

The one user-visible change is Ed25519Key.verifying_key, which now
returns an Ed25519PublicKey rather than a nacl.signing.VerifyKey.

Refs paramiko#1419, paramiko#2527. Builds on the earlier attempts by @adiroiban in
paramiko#1806 and @boyxuper in paramiko#2103.
toabctl added a commit to toabctl/paramiko that referenced this pull request Aug 26, 2026
Ed25519 support arrived in 2.2 (paramiko#325 via paramiko#972) backed by PyNaCl,
because Cryptography had no Ed25519 primitive at the time; it landed in
Cryptography 2.6, roughly two years later. Our floor is already 3.3, so
the primitive has been available across the entire supported range, and
Ed25519Key was the last remaining user of PyNaCl.

Beyond dropping a C-extension dependency, this puts Ed25519 under the
same OpenSSL configuration that already governs every other key type,
cipher and KEX we use -- including any algorithm policy the host
OpenSSL enforces, which a separately bundled libsodium cannot
participate in. Compare KexCurve25519.is_available(), which already
defers to Cryptography for exactly that reason.

Implementation notes:

* Uses public_bytes(Encoding.Raw, PublicFormat.Raw) rather than the
  tidier public_bytes_raw(), which only exists as of Cryptography 40.
  The older spelling works across our whole supported range, including
  the 3.3.2 and 39.0.0 cells in CI.

* Ed25519Key._fields now holds the key's raw bytes rather than the key
  object itself. Cryptography's key objects compare by identity on
  older versions (3.3.2, 39.0.0) and are unhashable on newer ones, so
  neither is safe for the equality and hashing contract PKey relies on
  here; raw bytes are exact on every version. test_ed25519_compare
  gains a hash() assertion so it covers both failure modes.

* Cryptography's verify() takes (signature, data), the reverse of
  PyNaCl's verify(smessage, signature).

Wire format is unchanged. Ed25519 is deterministic, so the new
test_sign_and_verify_ed25519 pins the exact signature bytes the PyNaCl
implementation produced for the existing test key.

The one user-visible change is Ed25519Key.verifying_key, which now
returns an Ed25519PublicKey rather than a nacl.signing.VerifyKey.

Refs paramiko#1419, paramiko#2527. Builds on the earlier attempts by @adiroiban in
paramiko#1806 and @boyxuper in paramiko#2103.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants