_util.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import sys
  2. import warnings
  3. from six import PY2, text_type
  4. from cryptography.hazmat.bindings.openssl.binding import Binding
  5. binding = Binding()
  6. binding.init_static_locks()
  7. ffi = binding.ffi
  8. lib = binding.lib
  9. # This is a special CFFI allocator that does not bother to zero its memory
  10. # after allocation. This has vastly better performance on large allocations and
  11. # so should be used whenever we don't need the memory zeroed out.
  12. no_zero_allocator = ffi.new_allocator(should_clear_after_alloc=False)
  13. def text(charp):
  14. """
  15. Get a native string type representing of the given CFFI ``char*`` object.
  16. :param charp: A C-style string represented using CFFI.
  17. :return: :class:`str`
  18. """
  19. if not charp:
  20. return ""
  21. return native(ffi.string(charp))
  22. def exception_from_error_queue(exception_type):
  23. """
  24. Convert an OpenSSL library failure into a Python exception.
  25. When a call to the native OpenSSL library fails, this is usually signalled
  26. by the return value, and an error code is stored in an error queue
  27. associated with the current thread. The err library provides functions to
  28. obtain these error codes and textual error messages.
  29. """
  30. errors = []
  31. while True:
  32. error = lib.ERR_get_error()
  33. if error == 0:
  34. break
  35. errors.append(
  36. (
  37. text(lib.ERR_lib_error_string(error)),
  38. text(lib.ERR_func_error_string(error)),
  39. text(lib.ERR_reason_error_string(error)),
  40. )
  41. )
  42. raise exception_type(errors)
  43. def make_assert(error):
  44. """
  45. Create an assert function that uses :func:`exception_from_error_queue` to
  46. raise an exception wrapped by *error*.
  47. """
  48. def openssl_assert(ok):
  49. """
  50. If *ok* is not True, retrieve the error from OpenSSL and raise it.
  51. """
  52. if ok is not True:
  53. exception_from_error_queue(error)
  54. return openssl_assert
  55. def native(s):
  56. """
  57. Convert :py:class:`bytes` or :py:class:`unicode` to the native
  58. :py:class:`str` type, using UTF-8 encoding if conversion is necessary.
  59. :raise UnicodeError: The input string is not UTF-8 decodeable.
  60. :raise TypeError: The input is neither :py:class:`bytes` nor
  61. :py:class:`unicode`.
  62. """
  63. if not isinstance(s, (bytes, text_type)):
  64. raise TypeError("%r is neither bytes nor unicode" % s)
  65. if PY2:
  66. if isinstance(s, text_type):
  67. return s.encode("utf-8")
  68. else:
  69. if isinstance(s, bytes):
  70. return s.decode("utf-8")
  71. return s
  72. def path_string(s):
  73. """
  74. Convert a Python string to a :py:class:`bytes` string identifying the same
  75. path and which can be passed into an OpenSSL API accepting a filename.
  76. :param s: An instance of :py:class:`bytes` or :py:class:`unicode`.
  77. :return: An instance of :py:class:`bytes`.
  78. """
  79. if isinstance(s, bytes):
  80. return s
  81. elif isinstance(s, text_type):
  82. return s.encode(sys.getfilesystemencoding())
  83. else:
  84. raise TypeError("Path must be represented as bytes or unicode string")
  85. if PY2:
  86. def byte_string(s):
  87. return s
  88. else:
  89. def byte_string(s):
  90. return s.encode("charmap")
  91. # A marker object to observe whether some optional arguments are passed any
  92. # value or not.
  93. UNSPECIFIED = object()
  94. _TEXT_WARNING = (
  95. text_type.__name__ + " for {0} is no longer accepted, use bytes"
  96. )
  97. def text_to_bytes_and_warn(label, obj):
  98. """
  99. If ``obj`` is text, emit a warning that it should be bytes instead and try
  100. to convert it to bytes automatically.
  101. :param str label: The name of the parameter from which ``obj`` was taken
  102. (so a developer can easily find the source of the problem and correct
  103. it).
  104. :return: If ``obj`` is the text string type, a ``bytes`` object giving the
  105. UTF-8 encoding of that text is returned. Otherwise, ``obj`` itself is
  106. returned.
  107. """
  108. if isinstance(obj, text_type):
  109. warnings.warn(
  110. _TEXT_WARNING.format(label),
  111. category=DeprecationWarning,
  112. stacklevel=3,
  113. )
  114. return obj.encode("utf-8")
  115. return obj