libpass 1.7.5.post0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. libpass-1.7.5.post0.dist-info/METADATA +81 -0
  2. libpass-1.7.5.post0.dist-info/RECORD +69 -0
  3. libpass-1.7.5.post0.dist-info/WHEEL +4 -0
  4. libpass-1.7.5.post0.dist-info/licenses/LICENSE +116 -0
  5. passlib/__init__.py +3 -0
  6. passlib/_data/wordsets/bip39.txt +2049 -0
  7. passlib/_data/wordsets/eff_long.txt +7776 -0
  8. passlib/_data/wordsets/eff_prefixed.txt +1296 -0
  9. passlib/_data/wordsets/eff_short.txt +1296 -0
  10. passlib/_setup/__init__.py +1 -0
  11. passlib/_setup/stamp.py +159 -0
  12. passlib/apache.py +1185 -0
  13. passlib/apps.py +260 -0
  14. passlib/context.py +2066 -0
  15. passlib/crypto/__init__.py +1 -0
  16. passlib/crypto/_blowfish/__init__.py +171 -0
  17. passlib/crypto/_blowfish/_gen_files.py +256 -0
  18. passlib/crypto/_blowfish/base.py +1239 -0
  19. passlib/crypto/_blowfish/unrolled.py +1691 -0
  20. passlib/crypto/_md4.py +249 -0
  21. passlib/crypto/des.py +2283 -0
  22. passlib/crypto/digest.py +1111 -0
  23. passlib/crypto/scrypt/__init__.py +299 -0
  24. passlib/crypto/scrypt/_builtin.py +254 -0
  25. passlib/crypto/scrypt/_gen_files.py +161 -0
  26. passlib/crypto/scrypt/_salsa.py +188 -0
  27. passlib/exc.py +432 -0
  28. passlib/ext/__init__.py +1 -0
  29. passlib/ext/django/__init__.py +6 -0
  30. passlib/ext/django/models.py +38 -0
  31. passlib/ext/django/utils.py +1326 -0
  32. passlib/handlers/__init__.py +1 -0
  33. passlib/handlers/argon2.py +1119 -0
  34. passlib/handlers/bcrypt.py +1217 -0
  35. passlib/handlers/cisco.py +450 -0
  36. passlib/handlers/des_crypt.py +642 -0
  37. passlib/handlers/digests.py +184 -0
  38. passlib/handlers/django.py +545 -0
  39. passlib/handlers/fshp.py +223 -0
  40. passlib/handlers/ldap_digests.py +379 -0
  41. passlib/handlers/md5_crypt.py +375 -0
  42. passlib/handlers/misc.py +224 -0
  43. passlib/handlers/mssql.py +258 -0
  44. passlib/handlers/mysql.py +136 -0
  45. passlib/handlers/oracle.py +177 -0
  46. passlib/handlers/pbkdf2.py +521 -0
  47. passlib/handlers/phpass.py +142 -0
  48. passlib/handlers/postgres.py +61 -0
  49. passlib/handlers/roundup.py +33 -0
  50. passlib/handlers/scram.py +589 -0
  51. passlib/handlers/scrypt.py +413 -0
  52. passlib/handlers/sha1_crypt.py +173 -0
  53. passlib/handlers/sha2_crypt.py +657 -0
  54. passlib/handlers/sun_md5_crypt.py +387 -0
  55. passlib/handlers/windows.py +349 -0
  56. passlib/hash.py +111 -0
  57. passlib/hosts.py +112 -0
  58. passlib/ifc.py +354 -0
  59. passlib/pwd.py +813 -0
  60. passlib/registry.py +583 -0
  61. passlib/totp.py +1968 -0
  62. passlib/utils/__init__.py +1221 -0
  63. passlib/utils/binary.py +897 -0
  64. passlib/utils/compat/__init__.py +200 -0
  65. passlib/utils/decor.py +234 -0
  66. passlib/utils/des.py +35 -0
  67. passlib/utils/handlers.py +2857 -0
  68. passlib/utils/md4.py +34 -0
  69. passlib/utils/pbkdf2.py +188 -0
@@ -0,0 +1,450 @@
1
+ """
2
+ passlib.handlers.cisco -- Cisco password hashes
3
+ """
4
+
5
+ # =============================================================================
6
+ # imports
7
+ # =============================================================================
8
+ # core
9
+ from binascii import hexlify, unhexlify
10
+ from hashlib import md5
11
+ import logging
12
+
13
+ log = logging.getLogger(__name__)
14
+ from warnings import warn
15
+
16
+ # site
17
+ # pkg
18
+ from passlib.utils import right_pad_string, to_unicode, repeat_string, to_bytes
19
+ from passlib.utils.binary import h64
20
+ import passlib.utils.handlers as uh
21
+
22
+ # local
23
+ __all__ = [
24
+ "cisco_pix",
25
+ "cisco_asa",
26
+ "cisco_type7",
27
+ ]
28
+
29
+ # =============================================================================
30
+ # utils
31
+ # =============================================================================
32
+
33
+ #: dummy bytes used by spoil_digest var in cisco_pix._calc_checksum()
34
+ _DUMMY_BYTES = b"\xff" * 32
35
+
36
+
37
+ # =============================================================================
38
+ # cisco pix firewall hash
39
+ # =============================================================================
40
+ class cisco_pix(uh.HasUserContext, uh.StaticHandler):
41
+ """
42
+ This class implements the password hash used by older Cisco PIX firewalls,
43
+ and follows the :ref:`password-hash-api`.
44
+ It does a single round of hashing, and relies on the username
45
+ as the salt.
46
+
47
+ This class only allows passwords <= 16 bytes, anything larger
48
+ will result in a :exc:`~passlib.exc.PasswordSizeError` if passed to :meth:`~cisco_pix.hash`,
49
+ and be silently rejected if passed to :meth:`~cisco_pix.verify`.
50
+
51
+ The :meth:`~passlib.ifc.PasswordHash.hash`,
52
+ :meth:`~passlib.ifc.PasswordHash.genhash`, and
53
+ :meth:`~passlib.ifc.PasswordHash.verify` methods
54
+ all support the following extra keyword:
55
+
56
+ :param str user:
57
+ String containing name of user account this password is associated with.
58
+
59
+ This is *required* in order to correctly hash passwords associated
60
+ with a user account on the Cisco device, as it is used to salt
61
+ the hash.
62
+
63
+ Conversely, this *must* be omitted or set to ``""`` in order to correctly
64
+ hash passwords which don't have an associated user account
65
+ (such as the "enable" password).
66
+
67
+ .. versionadded:: 1.6
68
+
69
+ .. versionchanged:: 1.7.1
70
+
71
+ Passwords > 16 bytes are now rejected / throw error instead of being silently truncated,
72
+ to match Cisco behavior. A number of :ref:`bugs <passlib-asa96-bug>` were fixed
73
+ which caused prior releases to generate unverifiable hashes in certain cases.
74
+ """
75
+
76
+ # ===================================================================
77
+ # class attrs
78
+ # ===================================================================
79
+
80
+ # --------------------
81
+ # PasswordHash
82
+ # --------------------
83
+ name = "cisco_pix"
84
+
85
+ truncate_size = 16
86
+
87
+ # NOTE: these are the default policy for PasswordHash,
88
+ # but want to set them explicitly for now.
89
+ truncate_error = True
90
+ truncate_verify_reject = True
91
+
92
+ # --------------------
93
+ # GenericHandler
94
+ # --------------------
95
+ checksum_size = 16
96
+ checksum_chars = uh.HASH64_CHARS
97
+
98
+ # --------------------
99
+ # custom
100
+ # --------------------
101
+
102
+ #: control flag signalling "cisco_asa" mode, set by cisco_asa class
103
+ _is_asa = False
104
+
105
+ # ===================================================================
106
+ # methods
107
+ # ===================================================================
108
+ def _calc_checksum(self, secret):
109
+ """
110
+ This function implements the "encrypted" hash format used by Cisco
111
+ PIX & ASA. It's behavior has been confirmed for ASA 9.6,
112
+ but is presumed correct for PIX & other ASA releases,
113
+ as it fits with known test vectors, and existing literature.
114
+
115
+ While nearly the same, the PIX & ASA hashes have slight differences,
116
+ so this function performs differently based on the _is_asa class flag.
117
+ Noteable changes from PIX to ASA include password size limit
118
+ increased from 16 -> 32, and other internal changes.
119
+ """
120
+ # select PIX vs or ASA mode
121
+ asa = self._is_asa
122
+
123
+ #
124
+ # encode secret
125
+ #
126
+ # per ASA 8.4 documentation,
127
+ # http://www.cisco.com/c/en/us/td/docs/security/asa/asa84/configuration/guide/asa_84_cli_config/ref_cli.html#Supported_Character_Sets,
128
+ # it supposedly uses UTF-8 -- though some double-encoding issues have
129
+ # been observed when trying to actually *set* a non-ascii password
130
+ # via ASDM, and access via SSH seems to strip 8-bit chars.
131
+ #
132
+ if isinstance(secret, str):
133
+ secret = secret.encode("utf-8")
134
+
135
+ #
136
+ # check if password too large
137
+ #
138
+ # Per ASA 9.6 changes listed in
139
+ # http://www.cisco.com/c/en/us/td/docs/security/asa/roadmap/asa_new_features.html,
140
+ # prior releases had a maximum limit of 32 characters.
141
+ # Testing with an ASA 9.6 system bears this out --
142
+ # setting 32-char password for a user account,
143
+ # and logins will fail if any chars are appended.
144
+ # (ASA 9.6 added new PBKDF2-based hash algorithm,
145
+ # which supports larger passwords).
146
+ #
147
+ # Per PIX documentation
148
+ # http://www.cisco.com/en/US/docs/security/pix/pix50/configuration/guide/commands.html,
149
+ # it would not allow passwords > 16 chars.
150
+ #
151
+ # Thus, we unconditionally throw a password size error here,
152
+ # as nothing valid can come from a larger password.
153
+ # NOTE: assuming PIX has same behavior, but at 16 char limit.
154
+ #
155
+ spoil_digest = None
156
+ if len(secret) > self.truncate_size:
157
+ if self.use_defaults:
158
+ # called from hash()
159
+ msg = "Password too long (%s allows at most %d bytes)" % (
160
+ self.name,
161
+ self.truncate_size,
162
+ )
163
+ raise uh.exc.PasswordSizeError(self.truncate_size, msg=msg)
164
+ else:
165
+ # called from verify() --
166
+ # We don't want to throw error, or return early,
167
+ # as that would let attacker know too much. Instead, we set a
168
+ # flag to add some dummy data into the md5 digest, so that
169
+ # output won't match truncated version of secret, or anything
170
+ # else that's fixed and predictable.
171
+ spoil_digest = secret + _DUMMY_BYTES
172
+
173
+ #
174
+ # append user to secret
175
+ #
176
+ # Policy appears to be:
177
+ #
178
+ # * Nothing appended for enable password (user = "")
179
+ #
180
+ # * ASA: If user present, but secret is >= 28 chars, nothing appended.
181
+ #
182
+ # * 1-2 byte users not allowed.
183
+ # DEVIATION: we're letting them through, and repeating their
184
+ # chars ala 3-char user, to simplify testing.
185
+ # Could issue warning in the future though.
186
+ #
187
+ # * 3 byte user has first char repeated, to pad to 4.
188
+ # (observed under ASA 9.6, assuming true elsewhere)
189
+ #
190
+ # * 4 byte users are used directly.
191
+ #
192
+ # * 5+ byte users are truncated to 4 bytes.
193
+ #
194
+ user = self.user
195
+ if user:
196
+ if isinstance(user, str):
197
+ user = user.encode("utf-8")
198
+ if not asa or len(secret) < 28:
199
+ secret += repeat_string(user, 4)
200
+
201
+ #
202
+ # pad / truncate result to limit
203
+ #
204
+ # While PIX always pads to 16 bytes, ASA increases to 32 bytes IFF
205
+ # secret+user > 16 bytes. This makes PIX & ASA have different results
206
+ # where secret size in range(13,16), and user is present --
207
+ # PIX will truncate to 16, ASA will truncate to 32.
208
+ #
209
+ if asa and len(secret) > 16:
210
+ pad_size = 32
211
+ else:
212
+ pad_size = 16
213
+ secret = right_pad_string(secret, pad_size)
214
+
215
+ #
216
+ # md5 digest
217
+ #
218
+ if spoil_digest:
219
+ # make sure digest won't match truncated version of secret
220
+ secret += spoil_digest
221
+ digest = md5(secret).digest()
222
+
223
+ #
224
+ # drop every 4th byte
225
+ # NOTE: guessing this was done because it makes output exactly
226
+ # 16 bytes, which may have been a general 'char password[]'
227
+ # size limit under PIX
228
+ #
229
+ digest = bytes(c for i, c in enumerate(digest) if (i + 1) & 3)
230
+
231
+ #
232
+ # encode using Hash64
233
+ #
234
+ return h64.encode_bytes(digest).decode("ascii")
235
+
236
+ # NOTE: works, but needs UTs.
237
+ # @classmethod
238
+ # def same_as_pix(cls, secret, user=""):
239
+ # """
240
+ # test whether (secret + user) combination should
241
+ # have the same hash under PIX and ASA.
242
+ #
243
+ # mainly present to help unittests.
244
+ # """
245
+ # # see _calc_checksum() above for details of this logic.
246
+ # size = len(to_bytes(secret, "utf-8"))
247
+ # if user and size < 28:
248
+ # size += 4
249
+ # return size < 17
250
+
251
+ # ===================================================================
252
+ # eoc
253
+ # ===================================================================
254
+
255
+
256
+ class cisco_asa(cisco_pix):
257
+ """
258
+ This class implements the password hash used by Cisco ASA/PIX 7.0 and newer (2005).
259
+ Aside from a different internal algorithm, it's use and format is identical
260
+ to the older :class:`cisco_pix` class.
261
+
262
+ For passwords less than 13 characters, this should be identical to :class:`!cisco_pix`,
263
+ but will generate a different hash for most larger inputs
264
+ (See the `Format & Algorithm`_ section for the details).
265
+
266
+ This class only allows passwords <= 32 bytes, anything larger
267
+ will result in a :exc:`~passlib.exc.PasswordSizeError` if passed to :meth:`~cisco_asa.hash`,
268
+ and be silently rejected if passed to :meth:`~cisco_asa.verify`.
269
+
270
+ .. versionadded:: 1.7
271
+
272
+ .. versionchanged:: 1.7.1
273
+
274
+ Passwords > 32 bytes are now rejected / throw error instead of being silently truncated,
275
+ to match Cisco behavior. A number of :ref:`bugs <passlib-asa96-bug>` were fixed
276
+ which caused prior releases to generate unverifiable hashes in certain cases.
277
+ """
278
+
279
+ # ===================================================================
280
+ # class attrs
281
+ # ===================================================================
282
+
283
+ # --------------------
284
+ # PasswordHash
285
+ # --------------------
286
+ name = "cisco_asa"
287
+
288
+ # --------------------
289
+ # TruncateMixin
290
+ # --------------------
291
+ truncate_size = 32
292
+
293
+ # --------------------
294
+ # cisco_pix
295
+ # --------------------
296
+ _is_asa = True
297
+
298
+ # ===================================================================
299
+ # eoc
300
+ # ===================================================================
301
+
302
+
303
+ # =============================================================================
304
+ # type 7
305
+ # =============================================================================
306
+ class cisco_type7(uh.GenericHandler):
307
+ """
308
+ This class implements the "Type 7" password encoding used by Cisco IOS,
309
+ and follows the :ref:`password-hash-api`.
310
+ It has a simple 4-5 bit salt, but is nonetheless a reversible encoding
311
+ instead of a real hash.
312
+
313
+ The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords:
314
+
315
+ :type salt: int
316
+ :param salt:
317
+ This may be an optional salt integer drawn from ``range(0,16)``.
318
+ If omitted, one will be chosen at random.
319
+
320
+ :type relaxed: bool
321
+ :param relaxed:
322
+ By default, providing an invalid value for one of the other
323
+ keywords will result in a :exc:`ValueError`. If ``relaxed=True``,
324
+ and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning`
325
+ will be issued instead. Correctable errors include
326
+ ``salt`` values that are out of range.
327
+
328
+ Note that while this class outputs digests in upper-case hexadecimal,
329
+ it will accept lower-case as well.
330
+
331
+ This class also provides the following additional method:
332
+
333
+ .. automethod:: decode
334
+ """
335
+
336
+ # ===================================================================
337
+ # class attrs
338
+ # ===================================================================
339
+
340
+ # --------------------
341
+ # PasswordHash
342
+ # --------------------
343
+ name = "cisco_type7"
344
+ setting_kwds = ("salt",)
345
+
346
+ # --------------------
347
+ # GenericHandler
348
+ # --------------------
349
+ checksum_chars = uh.UPPER_HEX_CHARS
350
+
351
+ # --------------------
352
+ # HasSalt
353
+ # --------------------
354
+
355
+ # NOTE: encoding could handle max_salt_value=99, but since key is only 52
356
+ # chars in size, not sure what appropriate behavior is for that edge case.
357
+ min_salt_value = 0
358
+ max_salt_value = 52
359
+
360
+ # ===================================================================
361
+ # methods
362
+ # ===================================================================
363
+ @classmethod
364
+ def using(cls, salt=None, **kwds):
365
+ subcls = super().using(**kwds)
366
+ if salt is not None:
367
+ salt = subcls._norm_salt(salt, relaxed=kwds.get("relaxed"))
368
+ subcls._generate_salt = staticmethod(lambda: salt)
369
+ return subcls
370
+
371
+ @classmethod
372
+ def from_string(cls, hash):
373
+ hash = to_unicode(hash, "ascii", "hash")
374
+ if len(hash) < 2:
375
+ raise uh.exc.InvalidHashError(cls)
376
+ salt = int(hash[:2]) # may throw ValueError
377
+ return cls(salt=salt, checksum=hash[2:].upper())
378
+
379
+ def __init__(self, salt=None, **kwds):
380
+ super().__init__(**kwds)
381
+ if salt is not None:
382
+ salt = self._norm_salt(salt)
383
+ elif self.use_defaults:
384
+ salt = self._generate_salt()
385
+ assert self._norm_salt(salt) == salt, "generated invalid salt: %r" % (salt,)
386
+ else:
387
+ raise TypeError("no salt specified")
388
+ self.salt = salt
389
+
390
+ @classmethod
391
+ def _norm_salt(cls, salt, relaxed=False):
392
+ """
393
+ validate & normalize salt value.
394
+ .. note::
395
+ the salt for this algorithm is an integer 0-52, not a string
396
+ """
397
+ if not isinstance(salt, int):
398
+ raise uh.exc.ExpectedTypeError(salt, "integer", "salt")
399
+ if 0 <= salt <= cls.max_salt_value:
400
+ return salt
401
+ msg = "salt/offset must be in 0..52 range"
402
+ if relaxed:
403
+ warn(msg, uh.PasslibHashWarning)
404
+ return 0 if salt < 0 else cls.max_salt_value
405
+ else:
406
+ raise ValueError(msg)
407
+
408
+ @staticmethod
409
+ def _generate_salt():
410
+ return uh.rng.randint(0, 15)
411
+
412
+ def to_string(self):
413
+ return "%02d%s" % (self.salt, self.checksum)
414
+
415
+ def _calc_checksum(self, secret):
416
+ # XXX: no idea what unicode policy is, but all examples are
417
+ # 7-bit ascii compatible, so using UTF-8
418
+ if isinstance(secret, str):
419
+ secret = secret.encode("utf-8")
420
+ return hexlify(self._cipher(secret, self.salt)).decode("ascii").upper()
421
+
422
+ @classmethod
423
+ def decode(cls, hash, encoding="utf-8"):
424
+ """decode hash, returning original password.
425
+
426
+ :arg hash: encoded password
427
+ :param encoding: optional encoding to use (defaults to ``UTF-8``).
428
+ :returns: password as unicode
429
+ """
430
+ self = cls.from_string(hash)
431
+ tmp = unhexlify(self.checksum.encode("ascii"))
432
+ raw = self._cipher(tmp, self.salt)
433
+ return raw.decode(encoding) if encoding else raw
434
+
435
+ # type7 uses a xor-based vingere variant, using the following secret key:
436
+ _key = "dsfd;kfoA,.iyewrkldJKDHSUBsgvca69834ncxv9873254k;fg87"
437
+
438
+ @classmethod
439
+ def _cipher(cls, data: bytes, salt: int):
440
+ """xor static key against data - encrypts & decrypts"""
441
+ key = cls._key
442
+ key_size = len(key)
443
+ return bytes(
444
+ value ^ ord(key[(salt + idx) % key_size]) for idx, value in enumerate(data)
445
+ )
446
+
447
+
448
+ # =============================================================================
449
+ # eof
450
+ # =============================================================================