_auth.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. """
  2. Implements auth methods
  3. """
  4. from .err import OperationalError
  5. try:
  6. from cryptography.hazmat.backends import default_backend
  7. from cryptography.hazmat.primitives import serialization, hashes
  8. from cryptography.hazmat.primitives.asymmetric import padding
  9. _have_cryptography = True
  10. except ImportError:
  11. _have_cryptography = False
  12. from functools import partial
  13. import hashlib
  14. DEBUG = False
  15. SCRAMBLE_LENGTH = 20
  16. sha1_new = partial(hashlib.new, "sha1")
  17. # mysql_native_password
  18. # https://dev.mysql.com/doc/internals/en/secure-password-authentication.html#packet-Authentication::Native41
  19. def scramble_native_password(password, message):
  20. """Scramble used for mysql_native_password"""
  21. if not password:
  22. return b""
  23. stage1 = sha1_new(password).digest()
  24. stage2 = sha1_new(stage1).digest()
  25. s = sha1_new()
  26. s.update(message[:SCRAMBLE_LENGTH])
  27. s.update(stage2)
  28. result = s.digest()
  29. return _my_crypt(result, stage1)
  30. def _my_crypt(message1, message2):
  31. result = bytearray(message1)
  32. for i in range(len(result)):
  33. result[i] ^= message2[i]
  34. return bytes(result)
  35. # MariaDB's client_ed25519-plugin
  36. # https://mariadb.com/kb/en/library/connection/#client_ed25519-plugin
  37. _nacl_bindings = False
  38. def _init_nacl():
  39. global _nacl_bindings
  40. try:
  41. from nacl import bindings
  42. _nacl_bindings = bindings
  43. except ImportError:
  44. raise RuntimeError(
  45. "'pynacl' package is required for ed25519_password auth method"
  46. )
  47. def _scalar_clamp(s32):
  48. ba = bytearray(s32)
  49. ba0 = bytes(bytearray([ba[0] & 248]))
  50. ba31 = bytes(bytearray([(ba[31] & 127) | 64]))
  51. return ba0 + bytes(s32[1:31]) + ba31
  52. def ed25519_password(password, scramble):
  53. """Sign a random scramble with elliptic curve Ed25519.
  54. Secret and public key are derived from password.
  55. """
  56. # variable names based on rfc8032 section-5.1.6
  57. #
  58. if not _nacl_bindings:
  59. _init_nacl()
  60. # h = SHA512(password)
  61. h = hashlib.sha512(password).digest()
  62. # s = prune(first_half(h))
  63. s = _scalar_clamp(h[:32])
  64. # r = SHA512(second_half(h) || M)
  65. r = hashlib.sha512(h[32:] + scramble).digest()
  66. # R = encoded point [r]B
  67. r = _nacl_bindings.crypto_core_ed25519_scalar_reduce(r)
  68. R = _nacl_bindings.crypto_scalarmult_ed25519_base_noclamp(r)
  69. # A = encoded point [s]B
  70. A = _nacl_bindings.crypto_scalarmult_ed25519_base_noclamp(s)
  71. # k = SHA512(R || A || M)
  72. k = hashlib.sha512(R + A + scramble).digest()
  73. # S = (k * s + r) mod L
  74. k = _nacl_bindings.crypto_core_ed25519_scalar_reduce(k)
  75. ks = _nacl_bindings.crypto_core_ed25519_scalar_mul(k, s)
  76. S = _nacl_bindings.crypto_core_ed25519_scalar_add(ks, r)
  77. # signature = R || S
  78. return R + S
  79. # sha256_password
  80. def _roundtrip(conn, send_data):
  81. conn.write_packet(send_data)
  82. pkt = conn._read_packet()
  83. pkt.check_error()
  84. return pkt
  85. def _xor_password(password, salt):
  86. # Trailing NUL character will be added in Auth Switch Request.
  87. # See https://github.com/mysql/mysql-server/blob/7d10c82196c8e45554f27c00681474a9fb86d137/sql/auth/sha2_password.cc#L939-L945
  88. salt = salt[:SCRAMBLE_LENGTH]
  89. password_bytes = bytearray(password)
  90. # salt = bytearray(salt) # for PY2 compat.
  91. salt_len = len(salt)
  92. for i in range(len(password_bytes)):
  93. password_bytes[i] ^= salt[i % salt_len]
  94. return bytes(password_bytes)
  95. def sha2_rsa_encrypt(password, salt, public_key):
  96. """Encrypt password with salt and public_key.
  97. Used for sha256_password and caching_sha2_password.
  98. """
  99. if not _have_cryptography:
  100. raise RuntimeError(
  101. "'cryptography' package is required for sha256_password or"
  102. + " caching_sha2_password auth methods"
  103. )
  104. message = _xor_password(password + b"\0", salt)
  105. rsa_key = serialization.load_pem_public_key(public_key, default_backend())
  106. return rsa_key.encrypt(
  107. message,
  108. padding.OAEP(
  109. mgf=padding.MGF1(algorithm=hashes.SHA1()),
  110. algorithm=hashes.SHA1(),
  111. label=None,
  112. ),
  113. )
  114. def sha256_password_auth(conn, pkt):
  115. if conn._secure:
  116. if DEBUG:
  117. print("sha256: Sending plain password")
  118. data = conn.password + b"\0"
  119. return _roundtrip(conn, data)
  120. if pkt.is_auth_switch_request():
  121. conn.salt = pkt.read_all()
  122. if not conn.server_public_key and conn.password:
  123. # Request server public key
  124. if DEBUG:
  125. print("sha256: Requesting server public key")
  126. pkt = _roundtrip(conn, b"\1")
  127. if pkt.is_extra_auth_data():
  128. conn.server_public_key = pkt._data[1:]
  129. if DEBUG:
  130. print("Received public key:\n", conn.server_public_key.decode("ascii"))
  131. if conn.password:
  132. if not conn.server_public_key:
  133. raise OperationalError("Couldn't receive server's public key")
  134. data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
  135. else:
  136. data = b""
  137. return _roundtrip(conn, data)
  138. def scramble_caching_sha2(password, nonce):
  139. # (bytes, bytes) -> bytes
  140. """Scramble algorithm used in cached_sha2_password fast path.
  141. XOR(SHA256(password), SHA256(SHA256(SHA256(password)), nonce))
  142. """
  143. if not password:
  144. return b""
  145. p1 = hashlib.sha256(password).digest()
  146. p2 = hashlib.sha256(p1).digest()
  147. p3 = hashlib.sha256(p2 + nonce).digest()
  148. res = bytearray(p1)
  149. for i in range(len(p3)):
  150. res[i] ^= p3[i]
  151. return bytes(res)
  152. def caching_sha2_password_auth(conn, pkt):
  153. # No password fast path
  154. if not conn.password:
  155. return _roundtrip(conn, b"")
  156. if pkt.is_auth_switch_request():
  157. # Try from fast auth
  158. if DEBUG:
  159. print("caching sha2: Trying fast path")
  160. conn.salt = pkt.read_all()
  161. scrambled = scramble_caching_sha2(conn.password, conn.salt)
  162. pkt = _roundtrip(conn, scrambled)
  163. # else: fast auth is tried in initial handshake
  164. if not pkt.is_extra_auth_data():
  165. raise OperationalError(
  166. "caching sha2: Unknown packet for fast auth: %s" % pkt._data[:1]
  167. )
  168. # magic numbers:
  169. # 2 - request public key
  170. # 3 - fast auth succeeded
  171. # 4 - need full auth
  172. pkt.advance(1)
  173. n = pkt.read_uint8()
  174. if n == 3:
  175. if DEBUG:
  176. print("caching sha2: succeeded by fast path.")
  177. pkt = conn._read_packet()
  178. pkt.check_error() # pkt must be OK packet
  179. return pkt
  180. if n != 4:
  181. raise OperationalError("caching sha2: Unknown result for fast auth: %s" % n)
  182. if DEBUG:
  183. print("caching sha2: Trying full auth...")
  184. if conn._secure:
  185. if DEBUG:
  186. print("caching sha2: Sending plain password via secure connection")
  187. return _roundtrip(conn, conn.password + b"\0")
  188. if not conn.server_public_key:
  189. pkt = _roundtrip(conn, b"\x02") # Request public key
  190. if not pkt.is_extra_auth_data():
  191. raise OperationalError(
  192. "caching sha2: Unknown packet for public key: %s" % pkt._data[:1]
  193. )
  194. conn.server_public_key = pkt._data[1:]
  195. if DEBUG:
  196. print(conn.server_public_key.decode("ascii"))
  197. data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
  198. pkt = _roundtrip(conn, data)