binding.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 annotations
  5. import os
  6. import sys
  7. import threading
  8. import types
  9. import typing
  10. import warnings
  11. import cryptography
  12. from cryptography.exceptions import InternalError
  13. from cryptography.hazmat.bindings._rust import _openssl, openssl
  14. from cryptography.hazmat.bindings.openssl._conditional import CONDITIONAL_NAMES
  15. def _openssl_assert(ok: bool) -> None:
  16. if not ok:
  17. errors = openssl.capture_error_stack()
  18. raise InternalError(
  19. "Unknown OpenSSL error. This error is commonly encountered when "
  20. "another library is not cleaning up the OpenSSL error stack. If "
  21. "you are using cryptography with another library that uses "
  22. "OpenSSL try disabling it before reporting a bug. Otherwise "
  23. "please file an issue at https://github.com/pyca/cryptography/"
  24. "issues with information on how to reproduce "
  25. f"this. ({errors!r})",
  26. errors,
  27. )
  28. def build_conditional_library(
  29. lib: typing.Any,
  30. conditional_names: dict[str, typing.Callable[[], list[str]]],
  31. ) -> typing.Any:
  32. conditional_lib = types.ModuleType("lib")
  33. conditional_lib._original_lib = lib # type: ignore[attr-defined]
  34. excluded_names = set()
  35. for condition, names_cb in conditional_names.items():
  36. if not getattr(lib, condition):
  37. excluded_names.update(names_cb())
  38. for attr in dir(lib):
  39. if attr not in excluded_names:
  40. setattr(conditional_lib, attr, getattr(lib, attr))
  41. return conditional_lib
  42. class Binding:
  43. """
  44. OpenSSL API wrapper.
  45. """
  46. lib: typing.ClassVar = None
  47. ffi = _openssl.ffi
  48. _lib_loaded = False
  49. _init_lock = threading.Lock()
  50. def __init__(self) -> None:
  51. self._ensure_ffi_initialized()
  52. @classmethod
  53. def _ensure_ffi_initialized(cls) -> None:
  54. with cls._init_lock:
  55. if not cls._lib_loaded:
  56. cls.lib = build_conditional_library(
  57. _openssl.lib, CONDITIONAL_NAMES
  58. )
  59. cls._lib_loaded = True
  60. @classmethod
  61. def init_static_locks(cls) -> None:
  62. cls._ensure_ffi_initialized()
  63. def _verify_package_version(version: str) -> None:
  64. # Occasionally we run into situations where the version of the Python
  65. # package does not match the version of the shared object that is loaded.
  66. # This may occur in environments where multiple versions of cryptography
  67. # are installed and available in the python path. To avoid errors cropping
  68. # up later this code checks that the currently imported package and the
  69. # shared object that were loaded have the same version and raise an
  70. # ImportError if they do not
  71. so_package_version = _openssl.ffi.string(
  72. _openssl.lib.CRYPTOGRAPHY_PACKAGE_VERSION
  73. )
  74. if version.encode("ascii") != so_package_version:
  75. raise ImportError(
  76. "The version of cryptography does not match the loaded "
  77. "shared object. This can happen if you have multiple copies of "
  78. "cryptography installed in your Python path. Please try creating "
  79. "a new virtual environment to resolve this issue. "
  80. f"Loaded python version: {version}, "
  81. f"shared object version: {so_package_version}"
  82. )
  83. _openssl_assert(
  84. _openssl.lib.OpenSSL_version_num() == openssl.openssl_version(),
  85. )
  86. _verify_package_version(cryptography.__version__)
  87. Binding.init_static_locks()
  88. if (
  89. sys.platform == "win32"
  90. and os.environ.get("PROCESSOR_ARCHITEW6432") is not None
  91. ):
  92. warnings.warn(
  93. "You are using cryptography on a 32-bit Python on a 64-bit Windows "
  94. "Operating System. Cryptography will be significantly faster if you "
  95. "switch to using a 64-bit Python.",
  96. UserWarning,
  97. stacklevel=2,
  98. )