base.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. import typing
  5. from cryptography.hazmat.backends import _get_backend
  6. from cryptography.hazmat.backends.interfaces import Backend
  7. from cryptography.hazmat.primitives.asymmetric import dh
  8. from cryptography.hazmat.primitives.asymmetric.types import (
  9. PRIVATE_KEY_TYPES,
  10. PUBLIC_KEY_TYPES,
  11. )
  12. def load_pem_private_key(
  13. data: bytes,
  14. password: typing.Optional[bytes],
  15. backend: typing.Optional[Backend] = None,
  16. ) -> PRIVATE_KEY_TYPES:
  17. backend = _get_backend(backend)
  18. return backend.load_pem_private_key(data, password)
  19. def load_pem_public_key(
  20. data: bytes, backend: typing.Optional[Backend] = None
  21. ) -> PUBLIC_KEY_TYPES:
  22. backend = _get_backend(backend)
  23. return backend.load_pem_public_key(data)
  24. def load_pem_parameters(
  25. data: bytes, backend: typing.Optional[Backend] = None
  26. ) -> "dh.DHParameters":
  27. backend = _get_backend(backend)
  28. return backend.load_pem_parameters(data)
  29. def load_der_private_key(
  30. data: bytes,
  31. password: typing.Optional[bytes],
  32. backend: typing.Optional[Backend] = None,
  33. ) -> PRIVATE_KEY_TYPES:
  34. backend = _get_backend(backend)
  35. return backend.load_der_private_key(data, password)
  36. def load_der_public_key(
  37. data: bytes, backend: typing.Optional[Backend] = None
  38. ) -> PUBLIC_KEY_TYPES:
  39. backend = _get_backend(backend)
  40. return backend.load_der_public_key(data)
  41. def load_der_parameters(
  42. data: bytes, backend: typing.Optional[Backend] = None
  43. ) -> "dh.DHParameters":
  44. backend = _get_backend(backend)
  45. return backend.load_der_parameters(data)