annotate roundup/password.py @ 7220:4d83f9f751ff

Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py 90% of the code for these two paths in encodePassword is identical. Combine the identical parts of the code. When password.py is called directly, it runs tests. But the sys.path is messed up so that roundup/cgi is imported during execution rather than pythonlib/cgi.py leading to an import error during setup. This issue does not occur if the tests are run under pytest.
author John Rouillard <rouilj@ieee.org>
date Sun, 12 Mar 2023 20:28:53 -0400
parents da751d3a2138
children 07ce4e4110f5
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
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
27
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
28 from base64 import b64encode, b64decode
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
29 from hashlib import md5, sha1, sha512
4982
9ba03348f923 Remove roundup/anypy/hashlib_.py
John Kristensen <john@jerrykan.com>
parents: 4760
diff changeset
30
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
31 import roundup.anypy.random_ as random_
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5415
diff changeset
32
6981
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
33 from roundup.anypy.strings import us2s, b2s, s2b
7167
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
34 from roundup.exceptions import RoundupException
6717
469ad03e6cb8 Ignore crypt deprication warning
John Rouillard <rouilj@ieee.org>
parents: 6626
diff changeset
35
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
36 try:
6717
469ad03e6cb8 Ignore crypt deprication warning
John Rouillard <rouilj@ieee.org>
parents: 6626
diff changeset
37 with warnings.catch_warnings():
469ad03e6cb8 Ignore crypt deprication warning
John Rouillard <rouilj@ieee.org>
parents: 6626
diff changeset
38 warnings.filterwarnings("ignore", category=DeprecationWarning)
469ad03e6cb8 Ignore crypt deprication warning
John Rouillard <rouilj@ieee.org>
parents: 6626
diff changeset
39 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
40 except ImportError:
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
41 crypt = None
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
42
5428
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
43 _bempty = b""
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
44 _bjoin = _bempty.join
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
45
7167
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
46 class ConfigNotSet(RoundupException):
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
47 pass
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
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):
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
50 if bytes == str:
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):
1f1899658115 Python 3 preparation: use bytes more in password handling.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
59 if bytes == str:
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
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
98 from struct import pack
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
99 from hmac import HMAC
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
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
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
5391
a391a071d045 Python 3 preparation: use range() instead of xrange().
Joseph Myers <jsm@polyomino.org.uk>
parents: 5378
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()
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
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]
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
130
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):
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
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 '''
6000
6c3826600610 Bandit - silence old hash warnings.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
140 shaval = sha1(password) # nosec
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
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
175 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
176 """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
177
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
178 :arg password: passphrase to use to generate key (if unicode,
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
179 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
180 :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
181 :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
182 :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
183
5483
3d0f71775e42 use PBKDF2 implementation from Python's hashlib, if available
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5454
diff changeset
184 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
185
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
186 :returns:
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
187 raw bytes of generated key
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
188 """
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5415
diff changeset
189 password = s2b(us2s(password))
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
190 if keylen > 40:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
191 # 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
192 # 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
193 # 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
194 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
195 if rounds < 1:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
196 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
197 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
198
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
199
1905
dc43e339e607 Centralised conversion of user-input data to hyperdb values
Richard Jones <richard@users.sourceforge.net>
parents: 1583
diff changeset
200 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
201 """ 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
202 pass
dc43e339e607 Centralised conversion of user-input data to hyperdb values
Richard Jones <richard@users.sourceforge.net>
parents: 1583
diff changeset
203
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
204
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
205 def pbkdf2_unpack(pbkdf2):
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
206 """ 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
207 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
208 """
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5415
diff changeset
209 pbkdf2 = us2s(pbkdf2)
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
210 try:
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
211 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
212 except ValueError:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
213 raise PasswordValueError("invalid PBKDF2 hash (wrong number of "
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
214 "separators)")
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
215 if rounds.startswith("0"):
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
216 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
217 try:
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
218 rounds = int(rounds)
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
219 except ValueError:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
220 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
221 raw_salt = h64decode(salt)
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
222 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
223
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
224
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
225 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
226 """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
227 """
1343
2e557762ee87 fixed handling of missing password [SF#655632]
Richard Jones <richard@users.sourceforge.net>
parents: 1231
diff changeset
228 if plaintext is None:
2e557762ee87 fixed handling of missing password [SF#655632]
Richard Jones <richard@users.sourceforge.net>
parents: 1231
diff changeset
229 plaintext = ""
7220
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
230 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
231 if other:
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
232 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
233 else:
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
234 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
235 salt = h64encode(raw_salt)
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
236 if config:
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
237 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
238
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
239 # if we are testing
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
240 if ("pytest" in sys.modules and
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
241 "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
242 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
243 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
244 else:
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
245 # 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
246 # 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
247 # 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
248 # 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
249 # 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
250 rounds = 1000
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
251 else:
7167
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
252 if ("pytest" in sys.modules and
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
253 "PYTEST_CURRENT_TEST" in os.environ):
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
254 # Set rounds to 1000 if no config is passed and
7183
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7177
diff changeset
255 # we are running within a pytest test.
7167
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
256 rounds = 1000
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
257 else:
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
258 import logging
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
259 # Log and abort. Initialize rounds and log (which
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
260 # will probably be ignored) with traceback in case
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
261 # ConfigNotSet exception is removed in the
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
262 # future.
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
263 rounds = 2000000
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
264 logger = logging.getLogger('roundup')
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
265 if sys.version_info[0] > 2:
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
266 logger.critical(
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
267 "encodePassword called without config.",
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
268 stack_info = True)
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
269 else:
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
270 import inspect, traceback
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
271 where = inspect.currentframe()
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
272 trace = traceback.format_stack(where)
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
273 logger.critical(
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
274 "encodePassword called without config. %s",
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
275 trace[:-1]
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
276 )
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
277 raise ConfigNotSet("encodePassword called without config.")
f6b24a8524cd Modify code to reduce runtime when testing
John Rouillard <rouilj@ieee.org>
parents: 7165
diff changeset
278
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
279 if rounds < 1000:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
280 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
281 if scheme == "PBKDF2S5":
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
282 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
283 else:
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(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
285 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
286 elif scheme == 'SSHA':
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
287 if other:
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
288 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
289 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
290 else:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
291 # new password
5053
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
292 # variable salt length
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
293 salt_len = random_.randbelow(52-36) + 36
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
294 salt = random_.token_bytes(salt_len)
5454
fbbcbfc6dad0 fix encoding for hash functions
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5428
diff changeset
295 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
296 elif scheme == 'SHA':
6000
6c3826600610 Bandit - silence old hash warnings.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
297 s = sha1(s2b(plaintext)).hexdigest() # nosec
2277
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
298 elif scheme == 'MD5':
6000
6c3826600610 Bandit - silence old hash warnings.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
299 s = md5(s2b(plaintext)).hexdigest() # nosec
6626
120b0bb05b6e issue2551191 - Module deprication PEP 594. crypt
John Rouillard <rouilj@ieee.org>
parents: 6146
diff changeset
300 elif scheme == 'crypt':
120b0bb05b6e issue2551191 - Module deprication PEP 594. crypt
John Rouillard <rouilj@ieee.org>
parents: 6146
diff changeset
301 if crypt is None:
120b0bb05b6e issue2551191 - Module deprication PEP 594. crypt
John Rouillard <rouilj@ieee.org>
parents: 6146
diff changeset
302 raise PasswordValueError(
120b0bb05b6e issue2551191 - Module deprication PEP 594. crypt
John Rouillard <rouilj@ieee.org>
parents: 6146
diff changeset
303 'Unsupported encryption scheme %r' % scheme)
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
304 if other is not None:
3434
1f860b50fa5f encodePassword: don't trim the salt string...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2277
diff changeset
305 salt = other
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
306 else:
5415
2d6a92c3e212 Python 3 preparation: use string.ascii_letters instead of string.letters.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5414
diff changeset
307 saltchars = './0123456789'+string.ascii_letters
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
308 salt = random_.choice(saltchars) + random_.choice(saltchars)
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
309 s = crypt.crypt(plaintext, salt)
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
310 elif scheme == 'plaintext':
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
311 s = plaintext
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
312 else:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
313 raise PasswordValueError('Unknown encryption scheme %r' % scheme)
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
314 return s
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
315
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
316
4760
efdce3d32698 Increase generated password length to 12 symbols.
anatoly techtonik <techtonik@gmail.com>
parents: 4683
diff changeset
317 def generatePassword(length=12):
5415
2d6a92c3e212 Python 3 preparation: use string.ascii_letters instead of string.letters.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5414
diff changeset
318 chars = string.ascii_letters+string.digits
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
319 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
320 # make sure there is at least one digit
5488
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
321 digitidx = random_.randbelow(length)
52cb53eedf77 reworked random number use
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5483
diff changeset
322 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
323 return ''.join(password)
1583
caae7d8934dc set new email rego user password to random string
Richard Jones <richard@users.sourceforge.net>
parents: 1343
diff changeset
324
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
325
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
326 class JournalPassword:
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
327 """ 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
328 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
329 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
330 encrypted password.
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
331 """
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
332 default_scheme = 'PBKDF2' # new encryptions use this scheme
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
333 pwre = re.compile(r'{(\w+)}(.+)')
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
334
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
335 def __init__(self, encrypted=''):
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
336 if isinstance(encrypted, self.__class__):
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
337 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
338 else:
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
339 m = self.pwre.match(encrypted)
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
340 if m:
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
341 self.scheme = m.group(1)
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
342 else:
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
343 self.scheme = self.default_scheme
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
344 self.password = ''
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
345
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
346 def dummystr(self):
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
347 """ 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
348 - reports scheme, but nothing else
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
349 """
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
350 return "{%s}*encrypted*" % (self.scheme,)
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 __str__ = dummystr
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
353
5414
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
354 def __eq__(self, other):
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
355 """Compare this password against another password."""
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
356 # 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
357 if isinstance(other, self.__class__):
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
358 if self.scheme != other.scheme:
5414
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
359 return False
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
360 return self.password == other.password
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
361
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
362 # assume password is plaintext
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
363 if self.password is None:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
364 raise ValueError('Password not set')
5414
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
365 return self.password == encodePassword(other, self.scheme,
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
366 self.password or None)
5414
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
367
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
368 def __ne__(self, other):
3fa026621f69 Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5391
diff changeset
369 return not self.__eq__(other)
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
370
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
371
4483
22bc0426e348 Second patch from issue2550688 -- with some changes:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4480
diff changeset
372 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
373 """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
374
2277
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
375 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
376 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
377 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
378 encoding scheme is identified.
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
379
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
380 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
381 {scheme}data
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
382
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
383 Example usage:
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
384 >>> p = Password('sekrit')
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
385 >>> p == 'sekrit'
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
386 1
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
387 >>> p != 'not 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 >>> 'sekrit' == p
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 >>> 'not sekrit' != p
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
392 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
393 """
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
394
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
395 deprecated_schemes = ["SHA", "MD5", "crypt", "plaintext"]
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
396 experimental_schemes = [ "PBKDF2S5" ]
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
397 known_schemes = ["PBKDF2", "SSHA"] + experimental_schemes + \
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
398 deprecated_schemes
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
399
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
400 def __init__(self, plaintext=None, scheme=None, encrypted=None,
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
401 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
402 """Call setPassword if plaintext is not None."""
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
403 if scheme is None:
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
404 scheme = self.default_scheme
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
405 if plaintext is not None:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
406 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
407 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
408 self.unpack(encrypted, scheme, strict=strict, config=config)
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
409 else:
3439
822a2719b81b keep plaintext password in Password object property (rfe [SF#1379447])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 3434
diff changeset
410 self.scheme = self.default_scheme
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
411 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
412 self.plaintext = None
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
413
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5490
diff changeset
414 def __repr__(self):
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5490
diff changeset
415 return self.__str__()
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5490
diff changeset
416
7165
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
417 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
418 """ 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
419 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
420 """
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
421 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
422 return True
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
423 rounds, salt, raw_salt, digest = pbkdf2_unpack(self.password)
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
424 if rounds < 1000:
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
425 return True
7165
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
426 if (self.scheme == "PBKDF2"):
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
427 new_rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
7184
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
428 if ("pytest" in sys.modules and
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
429 "PYTEST_CURRENT_TEST" in os.environ):
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
430 if ("PYTEST_USE_CONFIG" in os.environ):
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
431 new_rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
432 else:
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
433 # for testing
8b2287d850c8 Fix round check/settings in needs_migration
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
434 new_rounds = 1000
7165
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
435 if rounds < int(new_rounds):
970cd6d2b8ea issue2551251 - migrate pbkdf2 passwords if more rounds configured
John Rouillard <rouilj@ieee.org>
parents: 7163
diff changeset
436 return True
4484
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
437 return False
52e13bf0bb40 Add new config-option 'migrate_passwords' in section 'web'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4483
diff changeset
438
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
439 def unpack(self, encrypted, scheme=None, 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
440 """Set the password info from the scheme:<encryted info> string
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
441 (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
442 """
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
443 m = self.pwre.match(encrypted)
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
444 if m:
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
445 self.scheme = m.group(1)
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
446 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
447 self.plaintext = None
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
448 else:
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
449 # currently plaintext - encrypt
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
450 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
451 if strict and self.scheme not in self.known_schemes:
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
452 raise PasswordValueError("Unknown encryption scheme: %r" %
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
453 (self.scheme,))
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
454
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
455 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
456 """Sets encrypts plaintext."""
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
457 if scheme is None:
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
458 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
459 self.scheme = scheme
4486
693c75d56ebe Add new config-option 'password_pbkdf2_default_rounds'...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4485
diff changeset
460 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
461 self.plaintext = plaintext
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
462
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
463 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
464 """Stringify the encrypted password for database storage."""
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
465 if self.password is None:
5378
35ea9b1efc14 Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5356
diff changeset
466 raise ValueError('Password not set')
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
467 return '{%s}%s' % (self.scheme, self.password)
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
468
6981
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
469
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
470 def test_missing_crypt(config=None):
6981
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
471 p = encodePassword('sekrit', 'crypt') # noqa: F841 - test only
aa629aebac41 flake8 - import order, spacing
John Rouillard <rouilj@ieee.org>
parents: 6717
diff changeset
472
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
473
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
474 def test(config=None):
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
475 # 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
476 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
477 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
478 assert 'sekrit' == Password(encrypted=str(p))
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
479 assert p == 'sekrit'
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
480 assert p != 'not sekrit'
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
481 assert 'sekrit' == p
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
482 assert 'not sekrit' != p
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
483
2277
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
484 # MD5
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
485 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
486 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
487 assert 'sekrit' == Password(encrypted=str(p))
2277
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
488 assert p == 'sekrit'
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
489 assert p != 'not sekrit'
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
490 assert 'sekrit' == p
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
491 assert 'not sekrit' != p
c9e52addda42 added MD5 scheme for password hiding
Richard Jones <richard@users.sourceforge.net>
parents: 2098
diff changeset
492
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
493 # crypt
4683
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
494 if crypt: # not available on Windows
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 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
496 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
497 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
498 assert p == 'sekrit'
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
499 assert p != 'not sekrit'
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
500 assert 'sekrit' == p
2f66d44616ad windows: Fix failing password tests due to missing crypt module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
501 assert 'not sekrit' != p
1229
5c581b120738 added "crypt" password encoding...
Richard Jones <richard@users.sourceforge.net>
parents: 1090
diff changeset
502
5053
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
503 # SSHA
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
504 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
505 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
506 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
507 assert p == 'sekrit'
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
508 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
509 assert 'sekrit' == p
9792b18e0b19 issue 2550880: Ability to choose password store scheme and SSHA support.
John Rouillard <rouilj@ieee.org>
parents: 4982
diff changeset
510 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
511
4480
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
512 # PBKDF2 - low level function
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
513 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
514 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
515 assert k == unhexlify("5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddbc5e5142f708a31e2e62b1e13")
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
516
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
517 # PBKDF2 - hash function
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
518 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
519 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
520
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
521 # 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
522 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
523 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
524 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
525 assert p == 'sekrit'
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
526 assert p != 'not sekrit'
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
527 assert 'sekrit' == p
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
528 assert 'not sekrit' != p
1613754d2646 Fix first part of Password handling security issue2550688
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents: 4089
diff changeset
529
7201
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
530 # PBKDF2S5 - high level integration
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
531 p = Password('sekrit', 'PBKDF2S5', config=config)
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
532 print(p)
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
533 assert Password(encrypted=str(p)) == 'sekrit'
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
534 assert 'sekrit' == Password(encrypted=str(p))
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
535 assert p == 'sekrit'
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
536 assert p != 'not sekrit'
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
537 assert 'sekrit' == p
da751d3a2138 issue2551253 - Modify password PBKDF2 method to use SHA512
John Rouillard <rouilj@ieee.org>
parents: 7185
diff changeset
538 assert 'not sekrit' != p
6007
e27a240430b8 flake8 formatting changes.
John Rouillard <rouilj@ieee.org>
parents: 6000
diff changeset
539
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
540 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
541 # invoking this with:
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
542 # 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
543 # 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
544 # ['/path/to/./roundup',
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
545 # '/path/to/.',
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
546 # '/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
547 # 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
548 # when importing
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
549 # 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
550 # 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
551 # 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
552
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
553 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
554
4d83f9f751ff Refeactor PBKDF2 and PBKDF2S5 to reuse code; fix python password.py
John Rouillard <rouilj@ieee.org>
parents: 7201
diff changeset
555 # 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
556 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
557 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
558 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
559 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
560 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
561 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
562 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
563 exception = 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
564 assert 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
565 assert exception.__str__() == "Unsupported encryption scheme 'crypt'"
270
a4241ddd22d7 Added the Password property type.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
566
3434
1f860b50fa5f encodePassword: don't trim the salt string...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2277
diff changeset
567 # vim: set filetype=python sts=4 sw=4 et si :

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