base.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. from __future__ import absolute_import, division, print_function
  5. import abc
  6. from enum import Enum
  7. import six
  8. from cryptography import utils
  9. from cryptography.hazmat.backends import _get_backend
  10. def load_pem_private_key(data, password, backend=None):
  11. backend = _get_backend(backend)
  12. return backend.load_pem_private_key(data, password)
  13. def load_pem_public_key(data, backend=None):
  14. backend = _get_backend(backend)
  15. return backend.load_pem_public_key(data)
  16. def load_pem_parameters(data, backend=None):
  17. backend = _get_backend(backend)
  18. return backend.load_pem_parameters(data)
  19. def load_der_private_key(data, password, backend=None):
  20. backend = _get_backend(backend)
  21. return backend.load_der_private_key(data, password)
  22. def load_der_public_key(data, backend=None):
  23. backend = _get_backend(backend)
  24. return backend.load_der_public_key(data)
  25. def load_der_parameters(data, backend=None):
  26. backend = _get_backend(backend)
  27. return backend.load_der_parameters(data)
  28. class Encoding(Enum):
  29. PEM = "PEM"
  30. DER = "DER"
  31. OpenSSH = "OpenSSH"
  32. Raw = "Raw"
  33. X962 = "ANSI X9.62"
  34. SMIME = "S/MIME"
  35. class PrivateFormat(Enum):
  36. PKCS8 = "PKCS8"
  37. TraditionalOpenSSL = "TraditionalOpenSSL"
  38. Raw = "Raw"
  39. OpenSSH = "OpenSSH"
  40. class PublicFormat(Enum):
  41. SubjectPublicKeyInfo = "X.509 subjectPublicKeyInfo with PKCS#1"
  42. PKCS1 = "Raw PKCS#1"
  43. OpenSSH = "OpenSSH"
  44. Raw = "Raw"
  45. CompressedPoint = "X9.62 Compressed Point"
  46. UncompressedPoint = "X9.62 Uncompressed Point"
  47. class ParameterFormat(Enum):
  48. PKCS3 = "PKCS3"
  49. @six.add_metaclass(abc.ABCMeta)
  50. class KeySerializationEncryption(object):
  51. pass
  52. @utils.register_interface(KeySerializationEncryption)
  53. class BestAvailableEncryption(object):
  54. def __init__(self, password):
  55. if not isinstance(password, bytes) or len(password) == 0:
  56. raise ValueError("Password must be 1 or more bytes.")
  57. self.password = password
  58. @utils.register_interface(KeySerializationEncryption)
  59. class NoEncryption(object):
  60. pass