/
password.py
309 lines (238 loc) · 10.1 KB
/
password.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
##############################################################################
#
# Copyright (c) 2009 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Password managers
"""
__docformat__ = 'restructuredtext'
from base64 import urlsafe_b64encode
from base64 import urlsafe_b64decode
from os import urandom
from codecs import getencoder
try:
from hashlib import md5, sha1
except ImportError:
# Python 2.4
from md5 import new as md5
from sha import new as sha1
from zope.interface import implements
from zope.password.interfaces import IMatchingPasswordManager
_encoder = getencoder("utf-8")
class PlainTextPasswordManager(object):
"""Plain text password manager.
>>> from zope.interface.verify import verifyObject
>>> manager = PlainTextPasswordManager()
>>> verifyObject(IMatchingPasswordManager, manager)
True
>>> password = u"right \N{CYRILLIC CAPITAL LETTER A}"
>>> encoded = manager.encodePassword(password)
>>> encoded
u'right \u0410'
>>> manager.checkPassword(encoded, password)
True
>>> manager.checkPassword(encoded, password + u"wrong")
False
The plain text password manager *never* claims to implement the scheme,
because this would open a security hole, where a hash from a different
scheme could be used as-is as a plain-text password. Authentication code
that needs to support plain-text passwords need to explicitly check for
plain-text password matches after all other options have been tested for::
>>> manager.match(encoded)
False
"""
implements(IMatchingPasswordManager)
def encodePassword(self, password):
return password
def checkPassword(self, encoded_password, password):
return encoded_password == self.encodePassword(password)
def match(self, encoded_password):
# We always return False for PlainText because it was a) not encrypted
# and b) matching against actual encryption methods would result in
# the ability to authenticate with the un-encrypted hash as a password.
# For example, you should not be able to authenticate with a literal
# SSHA hash.
return False
class SSHAPasswordManager(PlainTextPasswordManager):
"""SSHA password manager.
SSHA is basically SHA1-encoding which also incorporates a salt
into the encoded string. This way, stored passwords are more
robust against dictionary attacks of attackers that could get
access to lists of encoded passwords.
SSHA is regularly used in LDAP databases and we should be
compatible with passwords used there.
>>> from zope.interface.verify import verifyObject
>>> manager = SSHAPasswordManager()
>>> verifyObject(IMatchingPasswordManager, manager)
True
>>> password = u"right \N{CYRILLIC CAPITAL LETTER A}"
>>> encoded = manager.encodePassword(password, salt="")
>>> encoded
'{SSHA}BLTuxxVMXzouxtKVb7gLgNxzdAI='
>>> manager.match(encoded)
True
>>> manager.checkPassword(encoded, password)
True
>>> manager.checkPassword(encoded, password + u"wrong")
False
Using the `slappasswd` utility to encode ``secret``, we get
``{SSHA}J4mrr3NQHXzLVaT0h9TuEWoJOrxeQ5lv`` as seeded hash.
Our password manager generates the same value when seeded with the
same salt, so we can be sure, our output is compatible with
standard LDAP tools that also use SSHA::
>>> from base64 import urlsafe_b64decode
>>> salt = urlsafe_b64decode('XkOZbw==')
>>> password = 'secret'
>>> encoded = manager.encodePassword(password, salt)
>>> encoded
'{SSHA}J4mrr3NQHXzLVaT0h9TuEWoJOrxeQ5lv'
>>> manager.checkPassword(encoded, password)
True
>>> manager.checkPassword(encoded, password + u"wrong")
False
>>> manager.encodePassword(password) != manager.encodePassword(password)
True
The password manager should be able to cope with unicode strings for input::
>>> passwd = u'foobar\u2211' # sigma-sign.
>>> manager.checkPassword(manager.encodePassword(passwd), passwd)
True
>>> manager.checkPassword(unicode(manager.encodePassword(passwd)), passwd)
True
The manager only claims to implement SSHA encodings, anything not starting
with the string {SSHA} returns False::
>>> manager.match('{MD5}someotherhash')
False
"""
def encodePassword(self, password, salt=None):
if salt is None:
salt = urandom(4)
hash = sha1(_encoder(password)[0])
hash.update(salt)
return '{SSHA}' + urlsafe_b64encode(hash.digest() + salt)
def checkPassword(self, encoded_password, password):
# urlsafe_b64decode() cannot handle unicode input string. We
# encode to ascii. This is safe as the encoded_password string
# should not contain non-ascii characters anyway.
encoded_password = encoded_password.encode('ascii')
byte_string = urlsafe_b64decode(encoded_password[6:])
salt = byte_string[20:]
return encoded_password == self.encodePassword(password, salt)
def match(self, encoded_password):
return encoded_password.startswith('{SSHA}')
class MD5PasswordManager(PlainTextPasswordManager):
"""MD5 password manager.
>>> from zope.interface.verify import verifyObject
>>> manager = MD5PasswordManager()
>>> verifyObject(IMatchingPasswordManager, manager)
True
>>> password = u"right \N{CYRILLIC CAPITAL LETTER A}"
>>> encoded = manager.encodePassword(password)
>>> encoded
'{MD5}86dddccec45db4599f1ac00018e54139'
>>> manager.match(encoded)
True
>>> manager.checkPassword(encoded, password)
True
>>> manager.checkPassword(encoded, password + u"wrong")
False
The old version of this password manager didn't add the {MD5} to
passwords. Let's check if it can work with old stored passwords.
>>> encoded = manager.encodePassword(password)
>>> encoded = encoded[5:]
>>> encoded
'86dddccec45db4599f1ac00018e54139'
>>> manager.checkPassword(encoded, password)
True
However, because the prefix is missing, the password manager cannot claim
to implement the scheme:
>>> manager.match(encoded)
False
A previous version of this manager also created a cosmetic salt, added
to the start of the hash, but otherwise not used in creating the hash
itself. To still support these 'hashed' passwords, only the last 32 bytes
of the pre-existing hash are used:
>>> manager.checkPassword('salt' + encoded, password)
True
"""
def encodePassword(self, password, salt=None):
# The salt argument only exists for backwards compatibility and is
# ignored on purpose.
return '{MD5}%s' % (md5(_encoder(password)[0]).hexdigest())
def checkPassword(self, encoded_password, password):
return encoded_password[-32:] == self.encodePassword(password)[-32:]
def match(self, encoded_password):
return encoded_password.startswith('{MD5}')
class SHA1PasswordManager(PlainTextPasswordManager):
"""SHA1 password manager.
>>> from zope.interface.verify import verifyObject
>>> manager = SHA1PasswordManager()
>>> verifyObject(IMatchingPasswordManager, manager)
True
>>> password = u"right \N{CYRILLIC CAPITAL LETTER A}"
>>> encoded = manager.encodePassword(password)
>>> encoded
'{SHA}04b4eec7154c5f3a2ec6d2956fb80b80dc737402'
>>> manager.match(encoded)
True
>>> manager.checkPassword(encoded, password)
True
>>> manager.checkPassword(encoded, password + u"wrong")
False
The old version of this password manager didn't add the {SHA} to
passwords. Let's check if it can work with old stored passwords.
>>> encoded = manager.encodePassword(password)
>>> encoded = encoded[5:]
>>> encoded
'04b4eec7154c5f3a2ec6d2956fb80b80dc737402'
>>> manager.checkPassword(encoded, password)
True
However, because the prefix is missing, the password manager cannot claim
to implement the scheme:
>>> manager.match(encoded)
False
A previous version of this manager also created a cosmetic salt, added
to the start of the hash, but otherwise not used in creating the hash
itself. To still support these 'hashed' passwords, only the last 40 bytes
of the pre-existing hash are used:
>>> manager.checkPassword('salt' + encoded, password)
True
Previously, this password manager used {SHA1} as a prefix, but this was
changed to be compatible with LDAP (RFC 2307). The old prefix is still
supported:
>>> password = u"right \N{CYRILLIC CAPITAL LETTER A}"
>>> encoded = '{SHA1}04b4eec7154c5f3a2ec6d2956fb80b80dc737402'
>>> manager.match(encoded)
True
>>> manager.checkPassword(encoded, password)
True
>>> manager.checkPassword(encoded, password + u"wrong")
False
"""
def encodePassword(self, password, salt=None):
# The salt argument only exists for backwards compatibility and is
# ignored on purpose.
return '{SHA}%s' % sha1(_encoder(password)[0]).hexdigest()
def checkPassword(self, encoded_password, password):
if self.match(encoded_password):
encoded = encoded_password[encoded_password.find('}') + 1:]
return encoded[-40:] == self.encodePassword(password)[5:]
return encoded_password[-40:] == self.encodePassword(password)[5:]
def match(self, encoded_password):
return (
encoded_password.startswith('{SHA}') or
encoded_password.startswith('{SHA1}'))
# Simple registry
managers = [
('Plain Text', PlainTextPasswordManager()),
('MD5', MD5PasswordManager()),
('SHA1', SHA1PasswordManager()),
('SSHA', SSHAPasswordManager()),
]