/gdata/tlslite/SharedKeyDB.py

http://radioappz.googlecode.com/ · Python · 58 lines · 23 code · 7 blank · 28 comment · 2 complexity · 542ece8c3690716ac3b9ced5505a59a4 MD5 · raw file

  1. """Class for storing shared keys."""
  2. from utils.cryptomath import *
  3. from utils.compat import *
  4. from mathtls import *
  5. from Session import Session
  6. from BaseDB import BaseDB
  7. class SharedKeyDB(BaseDB):
  8. """This class represent an in-memory or on-disk database of shared
  9. keys.
  10. A SharedKeyDB can be passed to a server handshake function to
  11. authenticate a client based on one of the shared keys.
  12. This class is thread-safe.
  13. """
  14. def __init__(self, filename=None):
  15. """Create a new SharedKeyDB.
  16. @type filename: str
  17. @param filename: Filename for an on-disk database, or None for
  18. an in-memory database. If the filename already exists, follow
  19. this with a call to open(). To create a new on-disk database,
  20. follow this with a call to create().
  21. """
  22. BaseDB.__init__(self, filename, "shared key")
  23. def _getItem(self, username, valueStr):
  24. session = Session()
  25. session._createSharedKey(username, valueStr)
  26. return session
  27. def __setitem__(self, username, sharedKey):
  28. """Add a shared key to the database.
  29. @type username: str
  30. @param username: The username to associate the shared key with.
  31. Must be less than or equal to 16 characters in length, and must
  32. not already be in the database.
  33. @type sharedKey: str
  34. @param sharedKey: The shared key to add. Must be less than 48
  35. characters in length.
  36. """
  37. BaseDB.__setitem__(self, username, sharedKey)
  38. def _setItem(self, username, value):
  39. if len(username)>16:
  40. raise ValueError("username too long")
  41. if len(value)>=48:
  42. raise ValueError("shared key too long")
  43. return value
  44. def _checkItem(self, value, username, param):
  45. newSession = self._getItem(username, param)
  46. return value.masterSecret == newSession.masterSecret