signer.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. from __future__ import annotations
  2. import collections.abc as cabc
  3. import hashlib
  4. import hmac
  5. import typing as t
  6. from .encoding import _base64_alphabet
  7. from .encoding import base64_decode
  8. from .encoding import base64_encode
  9. from .encoding import want_bytes
  10. from .exc import BadSignature
  11. class SigningAlgorithm:
  12. """Subclasses must implement :meth:`get_signature` to provide
  13. signature generation functionality.
  14. """
  15. def get_signature(self, key: bytes, value: bytes) -> bytes:
  16. """Returns the signature for the given key and value."""
  17. raise NotImplementedError()
  18. def verify_signature(self, key: bytes, value: bytes, sig: bytes) -> bool:
  19. """Verifies the given signature matches the expected
  20. signature.
  21. """
  22. return hmac.compare_digest(sig, self.get_signature(key, value))
  23. class NoneAlgorithm(SigningAlgorithm):
  24. """Provides an algorithm that does not perform any signing and
  25. returns an empty signature.
  26. """
  27. def get_signature(self, key: bytes, value: bytes) -> bytes:
  28. return b""
  29. def _lazy_sha1(string: bytes = b"") -> t.Any:
  30. """Don't access ``hashlib.sha1`` until runtime. FIPS builds may not include
  31. SHA-1, in which case the import and use as a default would fail before the
  32. developer can configure something else.
  33. """
  34. return hashlib.sha1(string)
  35. class HMACAlgorithm(SigningAlgorithm):
  36. """Provides signature generation using HMACs."""
  37. #: The digest method to use with the MAC algorithm. This defaults to
  38. #: SHA1, but can be changed to any other function in the hashlib
  39. #: module.
  40. default_digest_method: t.Any = staticmethod(_lazy_sha1)
  41. def __init__(self, digest_method: t.Any = None):
  42. if digest_method is None:
  43. digest_method = self.default_digest_method
  44. self.digest_method: t.Any = digest_method
  45. def get_signature(self, key: bytes, value: bytes) -> bytes:
  46. mac = hmac.new(key, msg=value, digestmod=self.digest_method)
  47. return mac.digest()
  48. def _make_keys_list(
  49. secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes],
  50. ) -> list[bytes]:
  51. if isinstance(secret_key, (str, bytes)):
  52. return [want_bytes(secret_key)]
  53. return [want_bytes(s) for s in secret_key] # pyright: ignore
  54. class Signer:
  55. """A signer securely signs bytes, then unsigns them to verify that
  56. the value hasn't been changed.
  57. The secret key should be a random string of ``bytes`` and should not
  58. be saved to code or version control. Different salts should be used
  59. to distinguish signing in different contexts. See :doc:`/concepts`
  60. for information about the security of the secret key and salt.
  61. :param secret_key: The secret key to sign and verify with. Can be a
  62. list of keys, oldest to newest, to support key rotation.
  63. :param salt: Extra key to combine with ``secret_key`` to distinguish
  64. signatures in different contexts.
  65. :param sep: Separator between the signature and value.
  66. :param key_derivation: How to derive the signing key from the secret
  67. key and salt. Possible values are ``concat``, ``django-concat``,
  68. or ``hmac``. Defaults to :attr:`default_key_derivation`, which
  69. defaults to ``django-concat``.
  70. :param digest_method: Hash function to use when generating the HMAC
  71. signature. Defaults to :attr:`default_digest_method`, which
  72. defaults to :func:`hashlib.sha1`. Note that the security of the
  73. hash alone doesn't apply when used intermediately in HMAC.
  74. :param algorithm: A :class:`SigningAlgorithm` instance to use
  75. instead of building a default :class:`HMACAlgorithm` with the
  76. ``digest_method``.
  77. .. versionchanged:: 2.0
  78. Added support for key rotation by passing a list to
  79. ``secret_key``.
  80. .. versionchanged:: 0.18
  81. ``algorithm`` was added as an argument to the class constructor.
  82. .. versionchanged:: 0.14
  83. ``key_derivation`` and ``digest_method`` were added as arguments
  84. to the class constructor.
  85. """
  86. #: The default digest method to use for the signer. The default is
  87. #: :func:`hashlib.sha1`, but can be changed to any :mod:`hashlib` or
  88. #: compatible object. Note that the security of the hash alone
  89. #: doesn't apply when used intermediately in HMAC.
  90. #:
  91. #: .. versionadded:: 0.14
  92. default_digest_method: t.Any = staticmethod(_lazy_sha1)
  93. #: The default scheme to use to derive the signing key from the
  94. #: secret key and salt. The default is ``django-concat``. Possible
  95. #: values are ``concat``, ``django-concat``, and ``hmac``.
  96. #:
  97. #: .. versionadded:: 0.14
  98. default_key_derivation: str = "django-concat"
  99. def __init__(
  100. self,
  101. secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes],
  102. salt: str | bytes | None = b"itsdangerous.Signer",
  103. sep: str | bytes = b".",
  104. key_derivation: str | None = None,
  105. digest_method: t.Any | None = None,
  106. algorithm: SigningAlgorithm | None = None,
  107. ):
  108. #: The list of secret keys to try for verifying signatures, from
  109. #: oldest to newest. The newest (last) key is used for signing.
  110. #:
  111. #: This allows a key rotation system to keep a list of allowed
  112. #: keys and remove expired ones.
  113. self.secret_keys: list[bytes] = _make_keys_list(secret_key)
  114. self.sep: bytes = want_bytes(sep)
  115. if self.sep in _base64_alphabet:
  116. raise ValueError(
  117. "The given separator cannot be used because it may be"
  118. " contained in the signature itself. ASCII letters,"
  119. " digits, and '-_=' must not be used."
  120. )
  121. if salt is not None:
  122. salt = want_bytes(salt)
  123. else:
  124. salt = b"itsdangerous.Signer"
  125. self.salt = salt
  126. if key_derivation is None:
  127. key_derivation = self.default_key_derivation
  128. self.key_derivation: str = key_derivation
  129. if digest_method is None:
  130. digest_method = self.default_digest_method
  131. self.digest_method: t.Any = digest_method
  132. if algorithm is None:
  133. algorithm = HMACAlgorithm(self.digest_method)
  134. self.algorithm: SigningAlgorithm = algorithm
  135. @property
  136. def secret_key(self) -> bytes:
  137. """The newest (last) entry in the :attr:`secret_keys` list. This
  138. is for compatibility from before key rotation support was added.
  139. """
  140. return self.secret_keys[-1]
  141. def derive_key(self, secret_key: str | bytes | None = None) -> bytes:
  142. """This method is called to derive the key. The default key
  143. derivation choices can be overridden here. Key derivation is not
  144. intended to be used as a security method to make a complex key
  145. out of a short password. Instead you should use large random
  146. secret keys.
  147. :param secret_key: A specific secret key to derive from.
  148. Defaults to the last item in :attr:`secret_keys`.
  149. .. versionchanged:: 2.0
  150. Added the ``secret_key`` parameter.
  151. """
  152. if secret_key is None:
  153. secret_key = self.secret_keys[-1]
  154. else:
  155. secret_key = want_bytes(secret_key)
  156. if self.key_derivation == "concat":
  157. return t.cast(bytes, self.digest_method(self.salt + secret_key).digest())
  158. elif self.key_derivation == "django-concat":
  159. return t.cast(
  160. bytes, self.digest_method(self.salt + b"signer" + secret_key).digest()
  161. )
  162. elif self.key_derivation == "hmac":
  163. mac = hmac.new(secret_key, digestmod=self.digest_method)
  164. mac.update(self.salt)
  165. return mac.digest()
  166. elif self.key_derivation == "none":
  167. return secret_key
  168. else:
  169. raise TypeError("Unknown key derivation method")
  170. def get_signature(self, value: str | bytes) -> bytes:
  171. """Returns the signature for the given value."""
  172. value = want_bytes(value)
  173. key = self.derive_key()
  174. sig = self.algorithm.get_signature(key, value)
  175. return base64_encode(sig)
  176. def sign(self, value: str | bytes) -> bytes:
  177. """Signs the given string."""
  178. value = want_bytes(value)
  179. return value + self.sep + self.get_signature(value)
  180. def verify_signature(self, value: str | bytes, sig: str | bytes) -> bool:
  181. """Verifies the signature for the given value."""
  182. try:
  183. sig = base64_decode(sig)
  184. except Exception:
  185. return False
  186. value = want_bytes(value)
  187. for secret_key in reversed(self.secret_keys):
  188. key = self.derive_key(secret_key)
  189. if self.algorithm.verify_signature(key, value, sig):
  190. return True
  191. return False
  192. def unsign(self, signed_value: str | bytes) -> bytes:
  193. """Unsigns the given string."""
  194. signed_value = want_bytes(signed_value)
  195. if self.sep not in signed_value:
  196. raise BadSignature(f"No {self.sep!r} found in value")
  197. value, sig = signed_value.rsplit(self.sep, 1)
  198. if self.verify_signature(value, sig):
  199. return value
  200. raise BadSignature(f"Signature {sig!r} does not match", payload=value)
  201. def validate(self, signed_value: str | bytes) -> bool:
  202. """Only validates the given signed value. Returns ``True`` if
  203. the signature exists and is valid.
  204. """
  205. try:
  206. self.unsign(signed_value)
  207. return True
  208. except BadSignature:
  209. return False