kex_curve25519.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import binascii
  2. import hashlib
  3. from cryptography.exceptions import UnsupportedAlgorithm
  4. from cryptography.hazmat.primitives import constant_time, serialization
  5. from cryptography.hazmat.primitives.asymmetric.x25519 import (
  6. X25519PrivateKey,
  7. X25519PublicKey,
  8. )
  9. from paramiko.message import Message
  10. from paramiko.py3compat import byte_chr, long
  11. from paramiko.ssh_exception import SSHException
  12. _MSG_KEXECDH_INIT, _MSG_KEXECDH_REPLY = range(30, 32)
  13. c_MSG_KEXECDH_INIT, c_MSG_KEXECDH_REPLY = [byte_chr(c) for c in range(30, 32)]
  14. class KexCurve25519(object):
  15. hash_algo = hashlib.sha256
  16. def __init__(self, transport):
  17. self.transport = transport
  18. self.key = None
  19. @classmethod
  20. def is_available(cls):
  21. try:
  22. X25519PrivateKey.generate()
  23. except UnsupportedAlgorithm:
  24. return False
  25. else:
  26. return True
  27. def _perform_exchange(self, peer_key):
  28. secret = self.key.exchange(peer_key)
  29. if constant_time.bytes_eq(secret, b"\x00" * 32):
  30. raise SSHException(
  31. "peer's curve25519 public value has wrong order"
  32. )
  33. return secret
  34. def start_kex(self):
  35. self.key = X25519PrivateKey.generate()
  36. if self.transport.server_mode:
  37. self.transport._expect_packet(_MSG_KEXECDH_INIT)
  38. return
  39. m = Message()
  40. m.add_byte(c_MSG_KEXECDH_INIT)
  41. m.add_string(
  42. self.key.public_key().public_bytes(
  43. serialization.Encoding.Raw, serialization.PublicFormat.Raw
  44. )
  45. )
  46. self.transport._send_message(m)
  47. self.transport._expect_packet(_MSG_KEXECDH_REPLY)
  48. def parse_next(self, ptype, m):
  49. if self.transport.server_mode and (ptype == _MSG_KEXECDH_INIT):
  50. return self._parse_kexecdh_init(m)
  51. elif not self.transport.server_mode and (ptype == _MSG_KEXECDH_REPLY):
  52. return self._parse_kexecdh_reply(m)
  53. raise SSHException(
  54. "KexCurve25519 asked to handle packet type {:d}".format(ptype)
  55. )
  56. def _parse_kexecdh_init(self, m):
  57. peer_key_bytes = m.get_string()
  58. peer_key = X25519PublicKey.from_public_bytes(peer_key_bytes)
  59. K = self._perform_exchange(peer_key)
  60. K = long(binascii.hexlify(K), 16)
  61. # compute exchange hash
  62. hm = Message()
  63. hm.add(
  64. self.transport.remote_version,
  65. self.transport.local_version,
  66. self.transport.remote_kex_init,
  67. self.transport.local_kex_init,
  68. )
  69. server_key_bytes = self.transport.get_server_key().asbytes()
  70. exchange_key_bytes = self.key.public_key().public_bytes(
  71. serialization.Encoding.Raw, serialization.PublicFormat.Raw
  72. )
  73. hm.add_string(server_key_bytes)
  74. hm.add_string(peer_key_bytes)
  75. hm.add_string(exchange_key_bytes)
  76. hm.add_mpint(K)
  77. H = self.hash_algo(hm.asbytes()).digest()
  78. self.transport._set_K_H(K, H)
  79. sig = self.transport.get_server_key().sign_ssh_data(
  80. H, self.transport.host_key_type
  81. )
  82. # construct reply
  83. m = Message()
  84. m.add_byte(c_MSG_KEXECDH_REPLY)
  85. m.add_string(server_key_bytes)
  86. m.add_string(exchange_key_bytes)
  87. m.add_string(sig)
  88. self.transport._send_message(m)
  89. self.transport._activate_outbound()
  90. def _parse_kexecdh_reply(self, m):
  91. peer_host_key_bytes = m.get_string()
  92. peer_key_bytes = m.get_string()
  93. sig = m.get_binary()
  94. peer_key = X25519PublicKey.from_public_bytes(peer_key_bytes)
  95. K = self._perform_exchange(peer_key)
  96. K = long(binascii.hexlify(K), 16)
  97. # compute exchange hash and verify signature
  98. hm = Message()
  99. hm.add(
  100. self.transport.local_version,
  101. self.transport.remote_version,
  102. self.transport.local_kex_init,
  103. self.transport.remote_kex_init,
  104. )
  105. hm.add_string(peer_host_key_bytes)
  106. hm.add_string(
  107. self.key.public_key().public_bytes(
  108. serialization.Encoding.Raw, serialization.PublicFormat.Raw
  109. )
  110. )
  111. hm.add_string(peer_key_bytes)
  112. hm.add_mpint(K)
  113. self.transport._set_K_H(K, self.hash_algo(hm.asbytes()).digest())
  114. self.transport._verify_key(peer_host_key_bytes, sig)
  115. self.transport._activate_outbound()