METADATA 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. Metadata-Version: 2.3
  2. Name: scramp
  3. Version: 1.4.5
  4. Summary: An implementation of the SCRAM protocol.
  5. Project-URL: Homepage, https://github.com/tlocke/scramp
  6. Author: The Contributors
  7. License: MIT No Attribution
  8. License-File: LICENSE
  9. Keywords: SASL,SCRAM,authentication
  10. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: Intended Audience :: Developers
  12. Classifier: License :: OSI Approved :: MIT No Attribution License (MIT-0)
  13. Classifier: Operating System :: OS Independent
  14. Classifier: Programming Language :: Python
  15. Classifier: Programming Language :: Python :: 3
  16. Classifier: Programming Language :: Python :: 3.8
  17. Classifier: Programming Language :: Python :: 3.9
  18. Classifier: Programming Language :: Python :: 3.10
  19. Classifier: Programming Language :: Python :: 3.11
  20. Classifier: Programming Language :: Python :: Implementation
  21. Classifier: Programming Language :: Python :: Implementation :: CPython
  22. Classifier: Programming Language :: Python :: Implementation :: PyPy
  23. Requires-Python: >=3.8
  24. Requires-Dist: asn1crypto>=1.5.1
  25. Description-Content-Type: text/markdown
  26. # Scramp
  27. A Python implementation of the [SCRAM authentication protocol](
  28. https://en.wikipedia.org/wiki/Salted_Challenge_Response_Authentication_Mechanism>).
  29. Scramp supports the following mechanisms:
  30. - SCRAM-SHA-1
  31. - SCRAM-SHA-1-PLUS
  32. - SCRAM-SHA-256
  33. - SCRAM-SHA-256-PLUS
  34. - SCRAM-SHA-512
  35. - SCRAM-SHA-512-PLUS
  36. - SCRAM-SHA3-512
  37. - SCRAM-SHA3-512-PLUS
  38. ## Installation
  39. - Create a virtual environment: `python3 -m venv venv`
  40. - Activate the virtual environment: `source venv/bin/activate`
  41. - Install: `pip install scramp`
  42. ## Examples
  43. ### Client and Server
  44. Here's an example using both the client and the server. It's a bit contrived as normally
  45. you'd be using either the client or server on its own.
  46. ```python
  47. >>> from scramp import ScramClient, ScramMechanism
  48. >>>
  49. >>> USERNAME = 'user'
  50. >>> PASSWORD = 'pencil'
  51. >>> MECHANISMS = ['SCRAM-SHA-256']
  52. >>>
  53. >>>
  54. >>> # Choose a mechanism for our server
  55. >>> m = ScramMechanism() # Default is SCRAM-SHA-256
  56. >>>
  57. >>> # On the server side we create the authentication information for each user
  58. >>> # and store it in an authentication database. We'll use a dict:
  59. >>> db = {}
  60. >>>
  61. >>> salt, stored_key, server_key, iteration_count = m.make_auth_info(PASSWORD)
  62. >>>
  63. >>> db[USERNAME] = salt, stored_key, server_key, iteration_count
  64. >>>
  65. >>> # Define your own function for retrieving the authentication information
  66. >>> # from the database given a username
  67. >>>
  68. >>> def auth_fn(username):
  69. ... return db[username]
  70. >>>
  71. >>> # Make the SCRAM server
  72. >>> s = m.make_server(auth_fn)
  73. >>>
  74. >>> # Now set up the client and carry out authentication with the server
  75. >>> c = ScramClient(MECHANISMS, USERNAME, PASSWORD)
  76. >>> cfirst = c.get_client_first()
  77. >>>
  78. >>> s.set_client_first(cfirst)
  79. >>> sfirst = s.get_server_first()
  80. >>>
  81. >>> c.set_server_first(sfirst)
  82. >>> cfinal = c.get_client_final()
  83. >>>
  84. >>> s.set_client_final(cfinal)
  85. >>> sfinal = s.get_server_final()
  86. >>>
  87. >>> c.set_server_final(sfinal)
  88. >>>
  89. >>> # If it all runs through without raising an exception, the authentication
  90. >>> # has succeeded
  91. ```
  92. ### Client only
  93. Here's an example using just the client. The client nonce is specified in order to give
  94. a reproducible example, but in production you'd omit the `c_nonce` parameter and let
  95. `ScramClient` generate a client nonce:
  96. ```python
  97. >>> from scramp import ScramClient
  98. >>>
  99. >>> USERNAME = 'user'
  100. >>> PASSWORD = 'pencil'
  101. >>> C_NONCE = 'rOprNGfwEbeRWgbNEkqO'
  102. >>> MECHANISMS = ['SCRAM-SHA-256']
  103. >>>
  104. >>> # Normally the c_nonce would be omitted, in which case ScramClient will
  105. >>> # generate the nonce itself.
  106. >>>
  107. >>> c = ScramClient(MECHANISMS, USERNAME, PASSWORD, c_nonce=C_NONCE)
  108. >>>
  109. >>> # Get the client first message and send it to the server
  110. >>> cfirst = c.get_client_first()
  111. >>> print(cfirst)
  112. n,,n=user,r=rOprNGfwEbeRWgbNEkqO
  113. >>>
  114. >>> # Set the first message from the server
  115. >>> c.set_server_first(
  116. ... 'r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,'
  117. ... 's=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096')
  118. >>>
  119. >>> # Get the client final message and send it to the server
  120. >>> cfinal = c.get_client_final()
  121. >>> print(cfinal)
  122. c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=
  123. >>>
  124. >>> # Set the final message from the server
  125. >>> c.set_server_final('v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=')
  126. >>>
  127. >>> # If it all runs through without raising an exception, the authentication
  128. >>> # has succeeded
  129. ```
  130. ### Server only
  131. Here's an example using just the server. The server nonce and salt is specified in order
  132. to give a reproducible example, but in production you'd omit the `s_nonce` and `salt`
  133. parameters and let Scramp generate them:
  134. ```python
  135. >>> from scramp import ScramMechanism
  136. >>>
  137. >>> USERNAME = 'user'
  138. >>> PASSWORD = 'pencil'
  139. >>> S_NONCE = '%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0'
  140. >>> SALT = b'[m\x99h\x9d\x125\x8e\xec\xa0K\x14\x126\xfa\x81'
  141. >>>
  142. >>> db = {}
  143. >>>
  144. >>> m = ScramMechanism()
  145. >>>
  146. >>> salt, stored_key, server_key, iteration_count = m.make_auth_info(
  147. ... PASSWORD, salt=SALT)
  148. >>>
  149. >>> db[USERNAME] = salt, stored_key, server_key, iteration_count
  150. >>>
  151. >>> # Define your own function for getting a password given a username
  152. >>> def auth_fn(username):
  153. ... return db[username]
  154. >>>
  155. >>> # Normally the s_nonce parameter would be omitted, in which case the
  156. >>> # server will generate the nonce itself.
  157. >>>
  158. >>> s = m.make_server(auth_fn, s_nonce=S_NONCE)
  159. >>>
  160. >>> # Set the first message from the client
  161. >>> s.set_client_first('n,,n=user,r=rOprNGfwEbeRWgbNEkqO')
  162. >>>
  163. >>> # Get the first server message, and send it to the client
  164. >>> sfirst = s.get_server_first()
  165. >>> print(sfirst)
  166. r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096
  167. >>>
  168. >>> # Set the final message from the client
  169. >>> s.set_client_final(
  170. ... 'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,'
  171. ... 'p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=')
  172. >>>
  173. >>> # Get the final server message and send it to the client
  174. >>> sfinal = s.get_server_final()
  175. >>> print(sfinal)
  176. v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=
  177. >>>
  178. >>> # If it all runs through without raising an exception, the authentication
  179. >>> # has succeeded
  180. ```
  181. ### Server only with passlib
  182. Here's an example using just the server and using the [passlib hashing library](
  183. https://passlib.readthedocs.io/en/stable/index.html). The server nonce and salt is
  184. specified in order to give a reproducible example, but in production you'd omit the
  185. `s_nonce` and `salt` parameters and let Scramp generate them:
  186. ```python
  187. >>> from scramp import ScramMechanism
  188. >>> from passlib.hash import scram
  189. >>>
  190. >>> USERNAME = 'user'
  191. >>> PASSWORD = 'pencil'
  192. >>> S_NONCE = '%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0'
  193. >>> SALT = b'[m\x99h\x9d\x125\x8e\xec\xa0K\x14\x126\xfa\x81'
  194. >>> ITERATION_COUNT = 4096
  195. >>>
  196. >>> db = {}
  197. >>> hash = scram.using(salt=SALT, rounds=ITERATION_COUNT).hash(PASSWORD)
  198. >>>
  199. >>> salt, iteration_count, digest = scram.extract_digest_info(hash, 'sha-256')
  200. >>>
  201. >>> stored_key, server_key = m.make_stored_server_keys(digest)
  202. >>>
  203. >>> db[USERNAME] = salt, stored_key, server_key, iteration_count
  204. >>>
  205. >>> # Define your own function for getting a password given a username
  206. >>> def auth_fn(username):
  207. ... return db[username]
  208. >>>
  209. >>> # Normally the s_nonce parameter would be omitted, in which case the
  210. >>> # server will generate the nonce itself.
  211. >>>
  212. >>> m = ScramMechanism()
  213. >>> s = m.make_server(auth_fn, s_nonce=S_NONCE)
  214. >>>
  215. >>> # Set the first message from the client
  216. >>> s.set_client_first('n,,n=user,r=rOprNGfwEbeRWgbNEkqO')
  217. >>>
  218. >>> # Get the first server message, and send it to the client
  219. >>> sfirst = s.get_server_first()
  220. >>> print(sfirst)
  221. r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096
  222. >>>
  223. >>> # Set the final message from the client
  224. >>> s.set_client_final(
  225. ... 'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,'
  226. ... 'p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=')
  227. >>>
  228. >>> # Get the final server message and send it to the client
  229. >>> sfinal = s.get_server_final()
  230. >>> print(sfinal)
  231. v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=
  232. >>>
  233. >>> # If it all runs through without raising an exception, the authentication
  234. >>> # has succeeded
  235. ```
  236. ### Server Error
  237. Here's an example of when setting a message from the client causes an error. The server
  238. nonce and salt is specified in order to give a reproducible example, but in production
  239. you'd omit the `s_nonce` and `salt` parameters and let Scramp generate them:
  240. ```python
  241. >>> from scramp import ScramException, ScramMechanism
  242. >>>
  243. >>> USERNAME = 'user'
  244. >>> PASSWORD = 'pencil'
  245. >>> S_NONCE = '%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0'
  246. >>> SALT = b'[m\x99h\x9d\x125\x8e\xec\xa0K\x14\x126\xfa\x81'
  247. >>>
  248. >>> db = {}
  249. >>>
  250. >>> m = ScramMechanism()
  251. >>>
  252. >>> salt, stored_key, server_key, iteration_count = m.make_auth_info(
  253. ... PASSWORD, salt=SALT)
  254. >>>
  255. >>> db[USERNAME] = salt, stored_key, server_key, iteration_count
  256. >>>
  257. >>> # Define your own function for getting a password given a username
  258. >>> def auth_fn(username):
  259. ... return db[username]
  260. >>>
  261. >>> # Normally the s_nonce parameter would be omitted, in which case the
  262. >>> # server will generate the nonce itself.
  263. >>>
  264. >>> s = m.make_server(auth_fn, s_nonce=S_NONCE)
  265. >>>
  266. >>> try:
  267. ... # Set the first message from the client
  268. ... s.set_client_first('p=tls-unique,,n=user,r=rOprNGfwEbeRWgbNEkqO')
  269. ... except ScramException as e:
  270. ... print(e)
  271. ... # Get the final server message and send it to the client
  272. ... sfinal = s.get_server_final()
  273. ... print(sfinal)
  274. Received GS2 flag 'p' which indicates that the client requires channel binding, but the server does not: channel-binding-not-supported
  275. e=channel-binding-not-supported
  276. ```
  277. ### Standards
  278. - [RFC 5802](https://tools.ietf.org/html/rfc5802>) Describes SCRAM.
  279. - [RFC 7677](https://datatracker.ietf.org/doc/html/rfc7677>) Registers SCRAM-SHA-256 and SCRAM-SHA-256-PLUS.
  280. - [draft-melnikov-scram-sha-512-02](https://datatracker.ietf.org/doc/html/draft-melnikov-scram-sha-512) Registers SCRAM-SHA-512 and SCRAM-SHA-512-PLUS.
  281. - [draft-melnikov-scram-sha3-512](https://datatracker.ietf.org/doc/html/draft-melnikov-scram-sha3-512) Registers SCRAM-SHA3-512 and SCRAM-SHA3-512-PLUS.
  282. - [RFC 5929](https://datatracker.ietf.org/doc/html/rfc5929) Channel Bindings for TLS.
  283. - [draft-ietf-kitten-tls-channel-bindings-for-tls13](https://datatracker.ietf.org/doc/html/draft-ietf-kitten-tls-channel-bindings-for-tls13>) Defines the `tls-exporter` channel binding, which is [not yet supported by Scramp](https://github.com/tlocke/scramp/issues/9).
  284. ## API Docs
  285. ### scramp.MECHANISMS
  286. A tuple of the supported mechanism names.
  287. ### scramp.ScramClient
  288. `ScramClient(mechanisms, username, password, channel_binding=None, c_nonce=None)`
  289. Constructor of the `scramp.ScramClient` class, with the following parameters:
  290. - `mechanisms` - A list or tuple of mechanism names. ScramClient will choose the most secure. If `cbind_data` is `None`, the '-PLUS' variants will be filtered out first. The chosen mechanism is available as the property `mechanism_name`.
  291. - `username`
  292. - `password`
  293. - `channel_binding` - Providing a value for this parameter allows channel binding to be used (ie. it lets you use mechanisms ending in '-PLUS'). The value for `channel_binding` is a tuple consisting of the channel binding name and the channel binding data. For example, if the channel binding name is `tls-unique`, the `channel_binding` parameter would be `('tls-unique', data)`, where `data` is obtained by calling [SSLSocket.get\_channel\_binding()](https://docs.python.org/3/library/ssl.html#ssl.SSLSocket.get_channel_binding). The convenience function `scramp.make_channel_binding()` can be used to create a channel binding tuple.
  294. - `c_nonce` - The client nonce. It's sometimes useful to set this when testing / debugging, but in production this should be omitted, in which case `ScramClient` will generate a client nonce.
  295. The `ScramClient` object has the following methods and properties:
  296. - `get_client_first()` - Get the client first message.
  297. - `set_server_first(message)` - Set the first message from the server.
  298. - `get_client_final()` - Get the final client message.
  299. - `set_server_final(message)` - Set the final message from the server.
  300. - `mechanism_name` - The mechanism chosen from the list given in the constructor.
  301. ### scramp.ScramMechanism
  302. `ScramMechanism(mechanism='SCRAM-SHA-256')`
  303. Constructor of the `ScramMechanism` class, with the following parameter:
  304. - `mechanism` - The SCRAM mechanism to use.
  305. The `ScramMechanism` object has the following methods and properties:
  306. - `make_auth_info(password, iteration_count=None, salt=None)` - Returns the tuple `(salt, stored_key, server_key, iteration_count)` which is stored in the authentication database on the server side. It has the following parameters:
  307. - `password` - The user's password as a `str`.
  308. - `iteration_count` - The rounds as an `int`. If `None` then use the minimum associated with the mechanism.
  309. - `salt` - It's sometimes useful to set this binary parameter when testing / debugging, but in production this should be omitted, in which case a salt will be generated.
  310. - `make_server(auth_fn, channel_binding=None, s_nonce=None)` - returns a `ScramServer` object. It takes the following parameters:
  311. - `auth_fn` This is a function provided by the programmer that has one parameter, a username of type `str` and returns returns the tuple `(salt, stored_key, server_key, iteration_count)`. Where `salt`, `stored_key` and `server_key` are of a binary type, and `iteration_count` is an `int`.
  312. - `channel_binding` - Providing a value for this parameter allows channel binding to be used (ie. it lets you use mechanisms ending in `-PLUS`). The value for `channel_binding` is a tuple consisting of the channel binding name and the channel binding data. For example, if the channel binding name is 'tls-unique', the `channel_binding` parameter would be `('tls-unique', data)`, where `data` is obtained by calling [SSLSocket.get\_channel\_binding()](https://docs.python.org/3/library/ssl.html#ssl.SSLSocket.get_channel_binding>). The convenience function `scramp.make_channel_binding()` can be used to create a channel binding tuple. If `channel_binding` is provided and the mechanism isn't a `-PLUS` variant, then the server will negotiate with the client to use the `-PLUS` variant if the client supports it, or otherwise to use the mechanism without channel binding.
  313. - `s_nonce` - The server nonce as a `str`. It's sometimes useful to set this when testing / debugging, but in production this should be omitted, in which case `ScramServer` will generate a server nonce.
  314. - `make_stored_server_keys(salted_password)` - Returns `(stored_key, server_key)` tuple of `bytes` objects given a salted password. This is useful if you want to use a separate hashing implementation from the one provided by Scramp. It takes the following parameter:
  315. - `salted_password` - A binary object representing the hashed password.
  316. - `iteration_count` - The minimum iteration count recommended for this mechanism.
  317. ### scramp.ScramServer
  318. The `ScramServer` object has the following methods:
  319. - `set_client_first(message)` - Set the first message from the client.
  320. - `get_server_first()` - Get the server first message.
  321. - `set_client_final(message)` - Set the final client message.
  322. - `get_server_final()` - Get the server final message.
  323. ### scramp.make\_channel\_binding(name, ssl\_socket)
  324. A helper function that makes a `channel_binding` tuple when given a channel binding name and an SSL socket. The parameters are:
  325. - `name` - A channel binding name such as 'tls-unique' or 'tls-server-end-point'.
  326. - `ssl_socket` - An instance of [ssl.SSLSocket](https://docs.python.org/3/library/ssl.html#ssl.SSLSocket).
  327. ## Testing
  328. - Activate the virtual environment: `source venv/bin/activate`
  329. - Install `tox`: `pip install tox`
  330. - Run `tox`: `tox`
  331. ## OpenSSF Scorecard
  332. It might be worth running the [OpenSSF Scorecard](https://securityscorecards.dev/):
  333. `sudo docker run -e GITHUB_AUTH_TOKEN=<auth_token> gcr.io/openssf/scorecard:stable --repo=github.com/tlocke/scramp`
  334. ## Doing A Release Of Scramp
  335. Run `tox` to make sure all tests pass, then update the release notes, then do:
  336. - `git tag -a x.y.z -m "version x.y.z"`
  337. - `rm -r dist`
  338. - `python -m build`
  339. - `twine upload dist/*`
  340. ## Release Notes
  341. ### Version 1.4.5, 2024-04-13
  342. - Drop support for Python 3.7, which means we're not dependent on `importlib-metadata` anymore.
  343. - Various changes to build system, docs and automated testing.
  344. ### Version 1.4.4, 2022-11-01
  345. - Tighten up parsing of messages to make sure that a `ScramException` is raised if a message is malformed.
  346. ### Version 1.4.3, 2022-10-26
  347. - The client now sends a gs2-cbind-flag of 'y' if the client supports channel binding, but thinks the server does not.
  348. ### Version 1.4.2, 2022-10-22
  349. - Switch to using the MIT-0 licence https://choosealicense.com/licenses/mit-0/
  350. - When creating a ScramClient, allow non ``-PLUS`` variants, even if a `channel_binding` parameter is provided. Previously this would raise and exception.
  351. ### Version 1.4.1, 2021-08-25
  352. - When using `make_channel_binding()` to create a tls-server-end-point channel binding, support certificates with hash algorithm of sha512.
  353. ### Version 1.4.0, 2021-03-28
  354. - Raise an exception if the client receives an error from the server.
  355. ### Version 1.3.0, 2021-03-28
  356. - As the specification allows, server errors are now sent to the client in the `server_final` message, an exception is still thrown as before.
  357. ### Version 1.2.2, 2021-02-13
  358. - Fix bug in generating the AuthMessage. It was incorrect when channel binding was used. So now Scramp supports channel binding.
  359. ### Version 1.2.1, 2021-02-07
  360. - Add support for channel binding.
  361. - Add support for SCRAM-SHA-512 and SCRAM-SHA3-512 and their channel binding variants.
  362. ### Version 1.2.0, 2020-05-30
  363. - This is a backwardly incompatible change on the server side, the client side will work as before. The idea of this change is to make it possible to have an authentication database. That is, the authentication information can be stored, and then retrieved when needed to authenticate the user.
  364. - In addition, it's now possible on the server side to use a third party hashing library such as passlib as the hashing implementation.
  365. ### Version 1.1.1, 2020-03-28
  366. - Add the README and LICENCE to the distribution.
  367. ### Version 1.1.0, 2019-02-24
  368. - Add support for the SCRAM-SHA-1 mechanism.
  369. ### Version 1.0.0, 2019-02-17
  370. - Implement the server side as well as the client side.
  371. ### Version 0.0.0, 2019-02-10
  372. - Copied SCRAM implementation from [pg8000](https://github.com/tlocke/pg8000). The idea is to make it a general SCRAM implemtation. Credit to the [Scrampy](https://github.com/cagdass/scrampy) project which I read through to help with this project. Also credit to the [passlib](https://github.com/efficks/passlib) project from which I copied the `saslprep` function.