mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +00:00
Properly use bcrypt:
- Don't crypto: use a library provided time-independent comparison. - Document details about max password length and other caveats.
This commit is contained in:
parent
8e2284a643
commit
c5ceae5819
@ -51,31 +51,37 @@ def add_user(username, passphrase, name='', email='', expert=False):
|
|||||||
|
|
||||||
def check_credentials(username, passphrase):
|
def check_credentials(username, passphrase):
|
||||||
"""Verifies credentials for username and passphrase.
|
"""Verifies credentials for username and passphrase.
|
||||||
Returns None on success or a string describing the error on failure"""
|
|
||||||
|
|
||||||
|
Returns None on success or a string describing the error on failure.
|
||||||
|
|
||||||
|
Handles passwords up to 4096 bytes:
|
||||||
|
|
||||||
|
>>> len("A" * 4096)
|
||||||
|
4096
|
||||||
|
>>> len(u"u|2603" * 682)
|
||||||
|
4092
|
||||||
|
|
||||||
|
"""
|
||||||
if not username or not passphrase:
|
if not username or not passphrase:
|
||||||
error = "No username or password."
|
error = "No username or password."
|
||||||
cfg.log(error)
|
cfg.log(error)
|
||||||
return error
|
return error
|
||||||
|
|
||||||
# hash the password whether the user exists, to foil timing
|
bad_authentication = "Bad user-name or password."
|
||||||
# side-channel attacks
|
hashed_password = None
|
||||||
try:
|
|
||||||
if username in cfg.users:
|
|
||||||
u = cfg.users[username]
|
|
||||||
pass_hash = bcrypt.encrypt(passphrase, salt=u['salt'])
|
|
||||||
else:
|
|
||||||
u = None
|
|
||||||
pass_hash = bcrypt.encrypt(passphrase)
|
|
||||||
except PasswordSizeError:
|
|
||||||
error = "Password is too long."
|
|
||||||
cfg.log(error)
|
|
||||||
return error
|
|
||||||
|
|
||||||
if u is None or u['passphrase'] != pass_hash:
|
if username in cfg.users:
|
||||||
error = "Bad user-name or password."
|
if "passphrase" in cfg.users[username]:
|
||||||
else:
|
hashed_password = cfg.users[username]['passphrase']
|
||||||
error = None
|
|
||||||
|
try:
|
||||||
|
# time-dependent comparison when non-ASCII characters are used.
|
||||||
|
if not bcrypt.verify(passphrase, hashed_password):
|
||||||
|
error = bad_authentication
|
||||||
|
else:
|
||||||
|
error = None
|
||||||
|
except PasswordSizeError:
|
||||||
|
error = bad_authentication
|
||||||
|
|
||||||
if error:
|
if error:
|
||||||
cfg.log(error)
|
cfg.log(error)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user