annotate roundup/password.py @ 8264:09e8d1a4c796

docs: clarify wording, fix index, add superseder link Make superseder, messages etc. properties index entries point to the right place. Link to description of using Superseder in the original overview. fix bad wording on boolean properties.
author John Rouillard <rouilj@ieee.org>
date Wed, 08 Jan 2025 11:39:54 -0500
parents 6bd11a73f2ed
children 98011edc6c60
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
1 #
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
3 # This module is free software, and you may redistribute it and/or modify
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
4 # under the same terms as Python, so long as this copyright message and
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
5 # disclaimer are retained in their original form.
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
6 #
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
10 # POSSIBILITY OF SUCH DAMAGE.
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
11 #
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
3434
1f860b50fa5f encodePassword: don't trim the salt string...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2277
diff changeset
17 #
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1905
diff changeset
18 """Password handling (encoding, decoding).
406
bdc2ea127ae9 Added module docstrings to all modules.
Jürgen Hermann <jhermann@users.sourceforge.net>
parents: 302
diff changeset
19 """
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1905
diff changeset
20 __docformat__ = 'restructuredtext'
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
21
7177
d787f7282ea3 Move imports to top of file out of test code path.
John Rouillard <rouilj@ieee.org>
parents: 7167
diff changeset
22 import os
6981
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
23 import re
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
24 import string
7177
d787f7282ea3 Move imports to top of file out of test code path.
John Rouillard <rouilj@ieee.org>
parents: 7167
diff changeset
25 import sys
6981
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
26 import warnings
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
27 from base64 import b64decode, b64encode
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
28 from hashlib import md5, sha1, sha512
4982
9ba03348f923 Remove roundup/anypy/hashlib_.py
John Kristensen <john@jerrykan.com>
parents: 4760
diff changeset
29
7403
770fffae8167 Fix random_ import to use from import rather than import as
John Rouillard <rouilj@ieee.org>
parents: 7300
diff changeset
30 from roundup.anypy import random_
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
31 from roundup.anypy.strings import b2s, s2b, us2s
7167
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
32 from roundup.exceptions import RoundupException
6717
469ad03e6cb8 Ignore crypt deprication warning
John Rouillard <rouilj@ieee.org>
parents: 6626
diff changeset
33
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
34 try:
6717
469ad03e6cb8 Ignore crypt deprication warning
John Rouillard <rouilj@ieee.org>
parents: 6626
diff changeset
35 with warnings.catch_warnings():
469ad03e6cb8 Ignore crypt deprication warning
John Rouillard <rouilj@ieee.org>
parents: 6626
diff changeset
36 warnings.filterwarnings("ignore", category=DeprecationWarning)
469ad03e6cb8 Ignore crypt deprication warning
John Rouillard <rouilj@ieee.org>
parents: 6626
diff changeset
37 import crypt
4089
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
38 except ImportError:
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
39 crypt = None
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
40
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
41 _bempty = b""
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
42 _bjoin = _bempty.join
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
43
7228
07ce4e4110f5 flake8 fixes: whitespace, remove unused imports
John Rouillard <rouilj@ieee.org>
parents: 7220
diff changeset
44
7167
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
45 class ConfigNotSet(RoundupException):
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
46 pass
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
47
7228
07ce4e4110f5 flake8 fixes: whitespace, remove unused imports
John Rouillard <rouilj@ieee.org>
parents: 7220
diff changeset
48
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
49 def bchr(c):
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
50 if bytes is str:
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
51 # Python 2.
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
52 return chr(c)
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
53 else:
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
54 # Python 3.
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
55 return bytes((c,))
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
56
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
57
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
58 def bord(c):
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
59 if bytes is str:
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
60 # Python 2.
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
61 return ord(c)
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
62 else:
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
63 # Python 3. Elements of bytes are integers.
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
64 return c
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
65
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
66
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
67 # NOTE: PBKDF2 hash is using this variant of base64 to minimize encoding size,
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
68 # and have charset that's compatible w/ unix crypt variants
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
69 def h64encode(data):
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
70 """encode using variant of base64"""
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
71 return b2s(b64encode(data, b"./").strip(b"=\n"))
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
72
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
73
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
74 def h64decode(data):
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
75 """decode using variant of base64"""
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
76 data = s2b(data)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
77 off = len(data) % 4
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
78 if off == 0:
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
79 return b64decode(data, b"./")
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
80 elif off == 1:
4683
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
81 raise ValueError("Invalid base64 input")
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
82 elif off == 2:
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
83 return b64decode(data + b"==", b"./")
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
84 else:
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
85 return b64decode(data + b"=", b"./")
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
86
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
87
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
88 try:
5483
3d0f71775e42 use PBKDF2 implementation from Python's hashlib, if available
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5454
diff changeset
89 from hashlib import pbkdf2_hmac
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
90
5483
3d0f71775e42 use PBKDF2 implementation from Python's hashlib, if available
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5454
diff changeset
91 def _pbkdf2(password, salt, rounds, keylen):
3d0f71775e42 use PBKDF2 implementation from Python's hashlib, if available
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5454
diff changeset
92 return pbkdf2_hmac('sha1', password, salt, rounds, keylen)
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
93
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
94 def _pbkdf2_sha512(password, salt, rounds, keylen):
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
95 return pbkdf2_hmac('sha512', password, salt, rounds, keylen)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
96 except ImportError:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
97 # no hashlib.pbkdf2_hmac - make our own pbkdf2 function
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
98 from hmac import HMAC
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
99 from struct import pack
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
100
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
101 def xor_bytes(left, right):
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
102 "perform bitwise-xor of two byte-strings"
6981
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
103 return _bjoin(bchr(bord(l) ^ bord(r))
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
104 for l, r in zip(left, right)) # noqa: E741
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
105
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
106 def _pbkdf2(password, salt, rounds, keylen, sha=sha1):
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
107 if sha not in [sha1, sha512]:
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
108 raise ValueError(
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
109 "Invalid sha value passed to _pbkdf2: %s" % sha)
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
110 if sha == sha512:
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
111 digest_size = 64 # sha512 generates 64-byte blocks.
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
112 else:
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
113 digest_size = 20 # sha1 generates 20-byte blocks
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
114
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
115 total_blocks = int((keylen + digest_size - 1) / digest_size)
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
116 hmac_template = HMAC(password, None, sha)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
117 out = _bempty
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
118 for i in range(1, total_blocks + 1):
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
119 hmac = hmac_template.copy()
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
120 hmac.update(salt + pack(">L", i))
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
121 block = tmp = hmac.digest()
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
122 for _j in range(rounds - 1):
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
123 hmac = hmac_template.copy()
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
124 hmac.update(tmp)
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
125 tmp = hmac.digest()
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
126 # TODO: need to speed up this call
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
127 block = xor_bytes(block, tmp)
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
128 out += block
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
129 return out[:keylen]
7228
07ce4e4110f5 flake8 fixes: whitespace, remove unused imports
John Rouillard <rouilj@ieee.org>
parents: 7220
diff changeset
130
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
131 def _pbkdf2_sha512(password, salt, rounds, keylen):
7228
07ce4e4110f5 flake8 fixes: whitespace, remove unused imports
John Rouillard <rouilj@ieee.org>
parents: 7220
diff changeset
132 return _pbkdf2(password, salt, rounds, keylen, sha=sha512)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
133
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
134
5053
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
135 def ssha(password, salt):
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
136 ''' Make ssha digest from password and salt.
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
137 Based on code of Roberto Aguilar <roberto@baremetal.io>
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
138 https://gist.github.com/rca/7217540
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
139 '''
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
140 shaval = sha1(password) # noqa: S324
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
141 shaval.update(salt)
6146
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
142 ssha_digest = b2s(b64encode(shaval.digest() + salt).strip())
5053
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
143 return ssha_digest
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
144
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
145
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
146 def pbkdf2_sha512(password, salt, rounds, keylen):
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
147 """PBKDF2-HMAC-SHA512 password-based key derivation
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
148
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
149 :arg password: passphrase to use to generate key (if unicode,
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
150 converted to utf-8)
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
151 :arg salt: salt bytes to use when generating key
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
152 :param rounds: number of rounds to use to generate key
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
153 :arg keylen: number of bytes to generate
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
154
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
155 If hashlib supports pbkdf2, uses it's implementation as backend.
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
156
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
157 Unlike pbkdf2, this uses sha512 not sha1 as it's hash.
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
158
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
159 :returns:
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
160 raw bytes of generated key
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
161 """
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
162 password = s2b(us2s(password))
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
163 if keylen > 64:
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
164 # This statement may be old. - not seeing issues in testing
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
165 # with keylen > 40.
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
166 #
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
167 # NOTE: pbkdf2 allows up to (2**31-1)*20 bytes,
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
168 # but m2crypto has issues on some platforms above 40,
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
169 # and such sizes aren't needed for a password hash anyways...
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
170 raise ValueError("key length too large")
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
171 if rounds < 1:
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
172 raise ValueError("rounds must be positive number")
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
173 return _pbkdf2_sha512(password, salt, rounds, keylen)
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
174
7228
07ce4e4110f5 flake8 fixes: whitespace, remove unused imports
John Rouillard <rouilj@ieee.org>
parents: 7220
diff changeset
175
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
176 def pbkdf2(password, salt, rounds, keylen):
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
177 """pkcs#5 password-based key derivation v2.0
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
178
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
179 :arg password: passphrase to use to generate key (if unicode,
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
180 converted to utf-8)
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
181 :arg salt: salt bytes to use when generating key
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
182 :param rounds: number of rounds to use to generate key
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
183 :arg keylen: number of bytes to generate
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
184
5483
3d0f71775e42 use PBKDF2 implementation from Python's hashlib, if available
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5454
diff changeset
185 If hashlib supports pbkdf2, uses it's implementation as backend.
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
186
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
187 :returns:
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
188 raw bytes of generated key
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
189 """
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5415
diff changeset
190 password = s2b(us2s(password))
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
191 if keylen > 40:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
192 # NOTE: pbkdf2 allows up to (2**31-1)*20 bytes,
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
193 # but m2crypto has issues on some platforms above 40,
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
194 # and such sizes aren't needed for a password hash anyways...
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
195 raise ValueError("key length too large")
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
196 if rounds < 1:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
197 raise ValueError("rounds must be positive number")
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
198 return _pbkdf2(password, salt, rounds, keylen)
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
199
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
200
1905
dc43e339e607 Centralised conversion of user-input data to hyperdb values
Richard Jones <richard@users.sourceforge.net>
parents: 1583
diff changeset
201 class PasswordValueError(ValueError):
4089
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
202 """ The password value is not valid """
1905
dc43e339e607 Centralised conversion of user-input data to hyperdb values
Richard Jones <richard@users.sourceforge.net>
parents: 1583
diff changeset
203 pass
dc43e339e607 Centralised conversion of user-input data to hyperdb values
Richard Jones <richard@users.sourceforge.net>
parents: 1583
diff changeset
204
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
205
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
206 def pbkdf2_unpack(pbkdf2):
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
207 """ unpack pbkdf2 encrypted password into parts,
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
208 assume it has format "{rounds}${salt}${digest}
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
209 """
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5415
diff changeset
210 pbkdf2 = us2s(pbkdf2)
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
211 try:
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
212 rounds, salt, digest = pbkdf2.split("$")
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
213 except ValueError:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
214 raise PasswordValueError("invalid PBKDF2 hash (wrong number of "
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
215 "separators)")
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
216 if rounds.startswith("0"):
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
217 raise PasswordValueError("invalid PBKDF2 hash (zero-padded rounds)")
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
218 try:
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
219 rounds = int(rounds)
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
220 except ValueError:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
221 raise PasswordValueError("invalid PBKDF2 hash (invalid rounds)")
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
222 raw_salt = h64decode(salt)
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
223 return rounds, salt, raw_salt, digest
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
224
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
225
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
226 def encodePassword(plaintext, scheme, other=None, config=None):
4089
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
227 """Encrypt the plaintext password.
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
228 """
1343
2e557762ee87 fixed handling of missing password [SF#655632]
Richard Jones <richard@users.sourceforge.net>
parents: 1231
diff changeset
229 if plaintext is None:
2e557762ee87 fixed handling of missing password [SF#655632]
Richard Jones <richard@users.sourceforge.net>
parents: 1231
diff changeset
230 plaintext = ""
7220
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
231 if scheme in ["PBKDF2", "PBKDF2S5"]: # all PBKDF schemes
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
232 if other:
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
233 rounds, salt, raw_salt, _digest = pbkdf2_unpack(other)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
234 else:
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
235 raw_salt = random_.token_bytes(20)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
236 salt = h64encode(raw_salt)
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
237 if config:
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
238 rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
7183
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
239
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
240 # if we are testing
7228
07ce4e4110f5 flake8 fixes: whitespace, remove unused imports
John Rouillard <rouilj@ieee.org>
parents: 7220
diff changeset
241 if ("pytest" in sys.modules and
7183
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
242 "PYTEST_CURRENT_TEST" in os.environ):
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
243 if ("PYTEST_USE_CONFIG" in os.environ):
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
244 rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
245 else:
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
246 # Use 1000 rounds unless the test signals it
7220
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
247 # wants the config number by setting
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
248 # PYTEST_USE_CONFIG. Using the production
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
249 # rounds value of 2,000,000 (for sha1) makes
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
250 # testing increase from 12 minutes to 1 hour in CI.
7183
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
251 rounds = 1000
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
252 elif ("pytest" in sys.modules and
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
253 "PYTEST_CURRENT_TEST" in os.environ):
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
254 # Set rounds to 1000 if no config is passed and
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
255 # we are running within a pytest test.
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
256 rounds = 1000
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
257 else:
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
258 import logging
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
259 # Log and abort. Initialize rounds and log (which
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
260 # will probably be ignored) with traceback in case
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
261 # ConfigNotSet exception is removed in the
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
262 # future.
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
263 rounds = 2000000
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
264 logger = logging.getLogger('roundup')
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
265 if sys.version_info[0] > 2:
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
266 logger.critical(
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
267 "encodePassword called without config.",
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
268 stack_info=True)
7167
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
269 else:
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
270 import inspect
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
271 import traceback
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
272
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
273 where = inspect.currentframe()
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
274 trace = traceback.format_stack(where)
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
275 logger.critical(
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
276 "encodePassword called without config. %s",
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
277 trace[:-1]
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
278 )
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
279 raise ConfigNotSet("encodePassword called without config.")
7167
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
280
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
281 if rounds < 1000:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
282 raise PasswordValueError("invalid PBKDF2 hash (rounds too low)")
7220
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
283 if scheme == "PBKDF2S5":
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
284 raw_digest = pbkdf2_sha512(plaintext, raw_salt, rounds, 64)
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
285 else:
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
286 raw_digest = pbkdf2(plaintext, raw_salt, rounds, 20)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
287 return "%d$%s$%s" % (rounds, salt, h64encode(raw_digest))
5053
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
288 elif scheme == 'SSHA':
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
289 if other:
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
290 raw_other = b64decode(other)
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
291 salt = raw_other[20:]
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
292 else:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
293 # new password
5053
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
294 # variable salt length
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
295 salt_len = random_.randbelow(52 - 36) + 36
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
296 salt = random_.token_bytes(salt_len)
5454
fbbcbfc6dad0 fix encoding for hash functions
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5428
diff changeset
297 s = ssha(s2b(plaintext), salt)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
298 elif scheme == 'SHA':
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
299 s = sha1(s2b(plaintext)).hexdigest() # noqa: S324
2277
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
300 elif scheme == 'MD5':
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
301 s = md5(s2b(plaintext)).hexdigest() # noqa: S324
6626
120b0bb05b6e issue2551191 - Module deprication PEP 594. crypt
John Rouillard <rouilj@ieee.org>
parents: 6146
diff changeset
302 elif scheme == 'crypt':
120b0bb05b6e issue2551191 - Module deprication PEP 594. crypt
John Rouillard <rouilj@ieee.org>
parents: 6146
diff changeset
303 if crypt is None:
120b0bb05b6e issue2551191 - Module deprication PEP 594. crypt
John Rouillard <rouilj@ieee.org>
parents: 6146
diff changeset
304 raise PasswordValueError(
120b0bb05b6e issue2551191 - Module deprication PEP 594. crypt
John Rouillard <rouilj@ieee.org>
parents: 6146
diff changeset
305 'Unsupported encryption scheme %r' % scheme)
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
306 if other is not None:
3434
1f860b50fa5f encodePassword: don't trim the salt string...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2277
diff changeset
307 salt = other
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
308 else:
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
309 saltchars = './0123456789' + string.ascii_letters
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
310 salt = random_.choice(saltchars) + random_.choice(saltchars)
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
311 s = crypt.crypt(plaintext, salt)
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
312 elif scheme == 'plaintext':
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
313 s = plaintext
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
314 else:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
315 raise PasswordValueError('Unknown encryption scheme %r' % scheme)
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
316 return s
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
317
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
318
4760
efdce3d32698 Increase generated password length to 12 symbols.
anatoly techtonik <techtonik@gmail.com>
parents: 4683
diff changeset
319 def generatePassword(length=12):
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
320 chars = string.ascii_letters + string.digits
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
321 password = [random_.choice(chars) for x in range(length - 1)]
4760
efdce3d32698 Increase generated password length to 12 symbols.
anatoly techtonik <techtonik@gmail.com>
parents: 4683
diff changeset
322 # make sure there is at least one digit
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
323 digitidx = random_.randbelow(length)
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
324 password[digitidx:digitidx] = [random_.choice(string.digits)]
4760
efdce3d32698 Increase generated password length to 12 symbols.
anatoly techtonik <techtonik@gmail.com>
parents: 4683
diff changeset
325 return ''.join(password)
1583
caae7d8934dc set new email rego user password to random string
Richard Jones <richard@users.sourceforge.net>
parents: 1343
diff changeset
326
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
327
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
328 class JournalPassword:
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
329 """ Password dummy instance intended for journal operation.
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
330 We do not store passwords in the journal any longer. The dummy
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
331 version only reads the encryption scheme from the given
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
332 encrypted password.
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
333 """
8239
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
334 default_scheme = 'PBKDF2S5' # new encryptions use this scheme
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
335 pwre = re.compile(r'{(\w+)}(.+)')
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
336
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
337 def __init__(self, encrypted=''):
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
338 if isinstance(encrypted, self.__class__):
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
339 self.scheme = encrypted.scheme or self.default_scheme
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
340 else:
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
341 m = self.pwre.match(encrypted)
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
342 if m:
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
343 self.scheme = m.group(1)
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
344 else:
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
345 self.scheme = self.default_scheme
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
346 self.password = ''
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
347
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
348 def dummystr(self):
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
349 """ return dummy string to store in journal
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
350 - reports scheme, but nothing else
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
351 """
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
352 return "{%s}*encrypted*" % (self.scheme,)
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
353
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
354 __str__ = dummystr
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
355
5414
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
356 def __eq__(self, other):
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
357 """Compare this password against another password."""
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
358 # check to see if we're comparing instances
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
359 if isinstance(other, self.__class__):
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
360 if self.scheme != other.scheme:
5414
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
361 return False
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
362 return self.password == other.password
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
363
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
364 # assume password is plaintext
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
365 if self.password is None:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
366 raise ValueError('Password not set')
5414
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
367 return self.password == encodePassword(other, self.scheme,
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
368 self.password or None)
5414
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
369
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
370 def __ne__(self, other):
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
371 return not self.__eq__(other)
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
372
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
373
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
374 class Password(JournalPassword):
4089
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
375 """The class encapsulates a Password property type value in the database.
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
376
2277
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
377 The encoding of the password is one if None, 'SHA', 'MD5' or 'plaintext'.
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
378 The encodePassword function is used to actually encode the password from
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
379 plaintext. The None encoding is used in legacy databases where no
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
380 encoding scheme is identified.
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
381
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
382 The scheme is stored with the encoded data in the database:
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
383 {scheme}data
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
384
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
385 Example usage:
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
386 >>> p = Password('sekrit')
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
387 >>> p == 'sekrit'
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
388 1
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
389 >>> p != 'not sekrit'
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
390 1
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
391 >>> 'sekrit' == p
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
392 1
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
393 >>> 'not sekrit' != p
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
394 1
4089
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
395 """
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
396
8239
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
397 deprecated_schemes = ["PBKDF2", "SSHA", "SHA", "MD5", "plaintext"]
7702
70a6ee453ddc fix: fix failing test when crypt is missing.
John Rouillard <rouilj@ieee.org>
parents: 7568
diff changeset
398 if crypt:
70a6ee453ddc fix: fix failing test when crypt is missing.
John Rouillard <rouilj@ieee.org>
parents: 7568
diff changeset
399 # place just before plaintext if crypt is available
70a6ee453ddc fix: fix failing test when crypt is missing.
John Rouillard <rouilj@ieee.org>
parents: 7568
diff changeset
400 deprecated_schemes.insert(-1, "crypt")
8239
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
401 experimental_schemes = []
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
402 known_schemes = ["PBKDF2S5"] + experimental_schemes + \
7568
a2ecc31c43ac flake8: correct continutation line indent
John Rouillard <rouilj@ieee.org>
parents: 7403
diff changeset
403 deprecated_schemes
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
404
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
405 def __init__(self, plaintext=None, scheme=None, encrypted=None,
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
406 strict=False, config=None):
4089
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
407 """Call setPassword if plaintext is not None."""
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
408 if scheme is None:
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
409 scheme = self.default_scheme
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
410 if plaintext is not None:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
411 self.setPassword(plaintext, scheme, config=config)
2098
18addf2a8596 Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents: 2005
diff changeset
412 elif encrypted is not None:
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
413 self.unpack(encrypted, scheme, strict=strict, config=config)
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
414 else:
3439
822a2719b81b keep plaintext password in Password object property (rfe [SF#1379447])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 3434
diff changeset
415 self.scheme = self.default_scheme
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
416 self.password = None
3439
822a2719b81b keep plaintext password in Password object property (rfe [SF#1379447])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 3434
diff changeset
417 self.plaintext = None
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
418
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5490
diff changeset
419 def __repr__(self):
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5490
diff changeset
420 return self.__str__()
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5490
diff changeset
421
7165
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
422 def needs_migration(self, config):
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
423 """ Password has insecure scheme or other insecure parameters
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
424 and needs migration to new password scheme
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
425 """
4485
95aace124a8e use idea from Eli Collins to use a list of deprecated password encoding schemes
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4484
diff changeset
426 if self.scheme in self.deprecated_schemes:
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
427 return True
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
428
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
429 rounds, _salt, _raw_salt, _digest = pbkdf2_unpack(self.password)
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
430
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
431 if rounds < 1000:
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
432 return True
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
433
7165
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
434 if (self.scheme == "PBKDF2"):
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
435 new_rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
7240
594b562ca99c flake8 cleanups
John Rouillard <rouilj@ieee.org>
parents: 7228
diff changeset
436 if ("pytest" in sys.modules and
7184
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
437 "PYTEST_CURRENT_TEST" in os.environ):
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
438 if ("PYTEST_USE_CONFIG" in os.environ):
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
439 new_rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
440 else:
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
441 # for testing
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
442 new_rounds = 1000
7165
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
443 if rounds < int(new_rounds):
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
444 return True
8239
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
445
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
446 if (self.scheme == "PBKDF2S5"):
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
447 new_rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
448 if ("pytest" in sys.modules and
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
449 "PYTEST_CURRENT_TEST" in os.environ):
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
450 if ("PYTEST_USE_CONFIG" in os.environ):
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
451 new_rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
452 else:
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
453 # for testing
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
454 new_rounds = 1000
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
455 if rounds < int(new_rounds):
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
456 return True
6bd11a73f2ed issue2551253. default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 7830
diff changeset
457
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
458 return False
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
459
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
460 def unpack(self, encrypted, scheme=None, strict=False, config=None):
7826
897c23876e9f doc: fix spelling in comment.
John Rouillard <rouilj@ieee.org>
parents: 7702
diff changeset
461 """Set the password info from the scheme:<encrypted info> string
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
462 (the inverse of __str__)
4089
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
463 """
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
464 m = self.pwre.match(encrypted)
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
465 if m:
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
466 self.scheme = m.group(1)
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
467 self.password = m.group(2)
3439
822a2719b81b keep plaintext password in Password object property (rfe [SF#1379447])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 3434
diff changeset
468 self.plaintext = None
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
469 else:
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
470 # currently plaintext - encrypt
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
471 self.setPassword(encrypted, scheme, config=config)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
472 if strict and self.scheme not in self.known_schemes:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
473 raise PasswordValueError("Unknown encryption scheme: %r" %
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
474 (self.scheme,))
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
475
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
476 def setPassword(self, plaintext, scheme=None, config=None):
4089
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
477 """Sets encrypts plaintext."""
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
478 if scheme is None:
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
479 scheme = self.default_scheme
3439
822a2719b81b keep plaintext password in Password object property (rfe [SF#1379447])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 3434
diff changeset
480 self.scheme = scheme
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
481 self.password = encodePassword(plaintext, scheme, config=config)
3439
822a2719b81b keep plaintext password in Password object property (rfe [SF#1379447])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 3434
diff changeset
482 self.plaintext = plaintext
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
483
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
484 def __str__(self):
4089
eddb82d0964c Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Richard Jones <richard@users.sourceforge.net>
parents: 3439
diff changeset
485 """Stringify the encrypted password for database storage."""
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
486 if self.password is None:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
487 raise ValueError('Password not set')
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
488 return '{%s}%s' % (self.scheme, self.password)
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
489
6981
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
490
7185
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
491 def test_missing_crypt(config=None):
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
492 _p = encodePassword('sekrit', 'crypt', config=config)
6981
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
493
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
494
7185
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
495 def test(config=None):
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
496 # ruff: noqa: S101 SIM300 - asserts are ok
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
497 # SHA
7185
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
498 p = Password('sekrit', config=config)
6146
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
499 assert Password(encrypted=str(p)) == 'sekrit'
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
500 assert 'sekrit' == Password(encrypted=str(p))
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
501 assert p == 'sekrit'
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
502 assert p != 'not sekrit'
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
503 assert 'sekrit' == p
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
504 assert 'not sekrit' != p
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
505
2277
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
506 # MD5
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
507 p = Password('sekrit', 'MD5', config=config)
6146
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
508 assert Password(encrypted=str(p)) == 'sekrit'
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
509 assert 'sekrit' == Password(encrypted=str(p))
2277
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
510 assert p == 'sekrit'
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
511 assert p != 'not sekrit'
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
512 assert 'sekrit' == p
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
513 assert 'not sekrit' != p
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
514
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
515 # crypt
4683
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
516 if crypt: # not available on Windows
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
517 p = Password('sekrit', 'crypt', config=config)
6146
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
518 assert Password(encrypted=str(p)) == 'sekrit'
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
519 assert 'sekrit' == Password(encrypted=str(p))
4683
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
520 assert p == 'sekrit'
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
521 assert p != 'not sekrit'
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
522 assert 'sekrit' == p
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
523 assert 'not sekrit' != p
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
524
5053
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
525 # SSHA
7830
1b326a3d76b4 chore(lint): cleanups from ruff.
John Rouillard <rouilj@ieee.org>
parents: 7826
diff changeset
526 p = Password('sekrit', 'SSHA', config=config)
6146
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
527 assert Password(encrypted=str(p)) == 'sekrit'
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
528 assert 'sekrit' == Password(encrypted=str(p))
5053
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
529 assert p == 'sekrit'
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
530 assert p != 'not sekrit'
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
531 assert 'sekrit' == p
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
532 assert 'not sekrit' != p
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
533
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
534 # PBKDF2 - low level function
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
535 from binascii import unhexlify
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
536 k = pbkdf2("password", b"ATHENA.MIT.EDUraeburn", 1200, 32)
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
537 assert k == unhexlify("5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddbc5e5142f708a31e2e62b1e13")
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
538
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
539 # PBKDF2 - hash function
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
540 h = "5000$7BvbBq.EZzz/O0HuwX3iP.nAG3s$g3oPnFFaga2BJaX5PoPRljl4XIE"
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
541 assert encodePassword("sekrit", "PBKDF2", h, config=config) == h
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
542
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
543 # PBKDF2 - high level integration
7185
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
544 p = Password('sekrit', 'PBKDF2', config=config)
6146
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
545 assert Password(encrypted=str(p)) == 'sekrit'
01e9634b81a4 fixed string encoding of SSHA encoded passwords in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 6007
diff changeset
546 assert 'sekrit' == Password(encrypted=str(p))
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
547 assert p == 'sekrit'
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
548 assert p != 'not sekrit'
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
549 assert 'sekrit' == p
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
550 assert 'not sekrit' != p
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
551
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
552 # PBKDF2S5 - high level integration
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
553 p = Password('sekrit', 'PBKDF2S5', config=config)
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
554 print(p)
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
555 assert Password(encrypted=str(p)) == 'sekrit'
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
556 assert 'sekrit' == Password(encrypted=str(p))
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
557 assert p == 'sekrit'
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
558 assert p != 'not sekrit'
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
559 assert 'sekrit' == p
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
560 assert 'not sekrit' != p
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
561
7240
594b562ca99c flake8 cleanups
John Rouillard <rouilj@ieee.org>
parents: 7228
diff changeset
562
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
563 if __name__ == '__main__':
7220
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
564 # invoking this with:
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
565 # PYTHONPATH=. python2 roundup/password.py
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
566 # or with python3, results in sys.path starting with:
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
567 # ['/path/to/./roundup',
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
568 # '/path/to/.',
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
569 # '/usr/lib/python2.7',
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
570 # which makes import roundup.anypy.html fail in python2
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
571 # when importing
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
572 # from cgi import escape as html_escape
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
573 # because cgi is not /usr/lib/python2.7/cgi but
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
574 # roundup/cgi. Modify the path to remove the bogus trailing /roundup
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
575
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
576 sys.path[0] = sys.path[0][:sys.path[0].rindex('/')]
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
577
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
578 # we continue with our regularly scheduled tests
7185
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
579 from roundup.configuration import CoreConfig
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
580 test(CoreConfig())
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
581 crypt = None
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
582 exception = None
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
583 try:
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
584 test_missing_crypt(CoreConfig())
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
585 except PasswordValueError as e:
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
586 exception = e
7240
594b562ca99c flake8 cleanups
John Rouillard <rouilj@ieee.org>
parents: 7228
diff changeset
587 assert exception is not None
7185
8e8d111fcdcd Make in file tests work again. Also allow manual testing without overriding PBKDF rounds
John Rouillard <rouilj@ieee.org>
parents: 7184
diff changeset
588 assert exception.__str__() == "Unsupported encryption scheme 'crypt'"
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
589
3434
1f860b50fa5f encodePassword: don't trim the salt string...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2277
diff changeset
590 # vim: set filetype=python sts=4 sw=4 et si :

Roundup Issue Tracker: http://roundup-tracker.org/