Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_util.py 3.5KB

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