|
1 | 1 | import copy |
2 | 2 | import datetime as dt |
3 | 3 | import hashlib |
| 4 | +import hmac |
| 5 | +import base64 |
4 | 6 | import logging |
5 | 7 |
|
6 | 8 | import click |
@@ -87,6 +89,14 @@ def create_md5_hash(rolename, value): |
87 | 89 | salted_input = (value + rolename).encode('utf-8') |
88 | 90 | return 'md5' + hashlib.md5(salted_input).hexdigest() |
89 | 91 |
|
| 92 | +def create_scram_hash(password, salt, iterations): |
| 93 | + digest = hashlib.pbkdf2_hmac("sha256", password.encode(), salt, iterations) |
| 94 | + client_key = hmac.digest(digest, "Client Key".encode(), hashlib.sha256) |
| 95 | + stored_key = base64.b64encode(hashlib.sha256(client_key).digest()).decode() |
| 96 | + server_key = base64.b64encode(hmac.digest(digest, "Server Key".encode(), hashlib.sha256)).decode() |
| 97 | + |
| 98 | + return "SCRAM-SHA-256${}:{}${}:{}".format(iterations, base64.b64encode(salt).decode(), stored_key, server_key) |
| 99 | + |
90 | 100 |
|
91 | 101 | def is_valid_forever(val): |
92 | 102 | if val is None or val == 'infinity': |
@@ -194,13 +204,47 @@ def get_attribute_value(self, attribute): |
194 | 204 | return value |
195 | 205 |
|
196 | 206 | def is_same_password(self, value): |
197 | | - """ Convert the input value into a postgres rolname-salted md5 hash and compare |
198 | | - it with the currently stored hash """ |
199 | | - if value is None: |
200 | | - return self.current_attributes.get('rolpassword') is None |
| 207 | + """ Compare stored password hash to the new password. |
| 208 | +
|
| 209 | + Returns ``True`` if both ``value`` and the stored password are ``None`` (empty) |
| 210 | + **or** ``value`` is a plain text password its hash matches the stored password |
| 211 | + hash **or** ``value`` is a hashed password and matches the stored password hash |
| 212 | + type and value. ``False`` in any other case. """ |
| 213 | + current_value = self.current_attributes.get('rolpassword') |
| 214 | + |
| 215 | + if current_value == value: |
| 216 | + return True |
| 217 | + |
| 218 | + if value is None or current_value is None: |
| 219 | + return False |
| 220 | + |
| 221 | + if current_value.startswith("SCRAM-SHA-256$"): |
| 222 | + if value.startswith("md5"): |
| 223 | + return False |
| 224 | + |
| 225 | + if value.startswith("SCRAM-SHA-256$"): |
| 226 | + return current_value == value |
| 227 | + |
| 228 | + hash_parts = current_value.split("$") |
| 229 | + |
| 230 | + iterations, salt = hash_parts[1].split(":") |
| 231 | + iterations = int(iterations) |
| 232 | + salt = base64.b64decode(salt) |
| 233 | + |
| 234 | + return current_value == create_scram_hash(value, salt, iterations) |
| 235 | + |
| 236 | + if current_value.startswith("md5"): |
| 237 | + if value.startswith("SCRAM-SHA-256$"): |
| 238 | + return False |
| 239 | + |
| 240 | + if value.startswith("md5"): |
| 241 | + return current_value == value |
| 242 | + |
| 243 | + return hmac.compare_digest(current_value, create_md5_hash(self.rolename, value)) |
201 | 244 |
|
202 | | - md5_hash = create_md5_hash(self.rolename, value) |
203 | | - return self.current_attributes.get('rolpassword') == md5_hash |
| 245 | + # There’s currently only two hash algorithm supported by Postgres (md5 |
| 246 | + # and SCRAM-SHA-256) so we should never reach this. |
| 247 | + return False |
204 | 248 |
|
205 | 249 | def role_exists(self): |
206 | 250 | # If current_attributes is empty then the rolname wasn't in pg_authid, i.e. it doesn't exist |
|
0 commit comments