utils.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  5. # Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. import ssl
  20. from importlib import import_module, metadata
  21. from typing import Callable, Sequence, Type, TypeVar, Union
  22. from radicale import config
  23. from radicale.log import logger
  24. _T_co = TypeVar("_T_co", covariant=True)
  25. def load_plugin(internal_types: Sequence[str], module_name: str,
  26. class_name: str, base_class: Type[_T_co],
  27. configuration: "config.Configuration") -> _T_co:
  28. type_: Union[str, Callable] = configuration.get(module_name, "type")
  29. if callable(type_):
  30. logger.info("%s type is %r", module_name, type_)
  31. return type_(configuration)
  32. if type_ in internal_types:
  33. module = "radicale.%s.%s" % (module_name, type_)
  34. else:
  35. module = type_
  36. try:
  37. class_ = getattr(import_module(module), class_name)
  38. except Exception as e:
  39. raise RuntimeError("Failed to load %s module %r: %s" %
  40. (module_name, module, e)) from e
  41. logger.info("%s type is %r", module_name, module)
  42. return class_(configuration)
  43. def package_version(name):
  44. return metadata.version(name)
  45. def ssl_context_options_by_protocol(protocol: str, ssl_context_options):
  46. logger.debug("SSL protocol string: '%s' and current SSL context options: '0x%x'", protocol, ssl_context_options)
  47. # disable any protocol by default
  48. logger.debug("SSL context options, disable ALL by default")
  49. ssl_context_options |= ssl.OP_NO_SSLv2
  50. ssl_context_options |= ssl.OP_NO_SSLv3
  51. ssl_context_options |= ssl.OP_NO_TLSv1
  52. ssl_context_options |= ssl.OP_NO_TLSv1_1
  53. ssl_context_options |= ssl.OP_NO_TLSv1_2
  54. ssl_context_options |= ssl.OP_NO_TLSv1_3
  55. logger.debug("SSL cleared SSL context options: '0x%x'", ssl_context_options)
  56. for entry in protocol.split():
  57. entry = entry.strip('+') # remove trailing '+'
  58. if entry == "ALL":
  59. logger.debug("SSL context options, enable ALL (some maybe not supported by underlying OpenSSL, SSLv2 not enabled at all)")
  60. ssl_context_options &= ~ssl.OP_NO_SSLv3
  61. ssl_context_options &= ~ssl.OP_NO_TLSv1
  62. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  63. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  64. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  65. elif entry == "SSLv2":
  66. logger.warning("SSL context options, ignore SSLv2 (totally insecure)")
  67. elif entry == "SSLv3":
  68. ssl_context_options &= ~ssl.OP_NO_SSLv3
  69. logger.debug("SSL context options, enable SSLv3 (maybe not supported by underlying OpenSSL)")
  70. elif entry == "TLSv1":
  71. ssl_context_options &= ~ssl.OP_NO_TLSv1
  72. logger.debug("SSL context options, enable TLSv1 (maybe not supported by underlying OpenSSL)")
  73. elif entry == "TLSv1.1":
  74. logger.debug("SSL context options, enable TLSv1.1 (maybe not supported by underlying OpenSSL)")
  75. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  76. elif entry == "TLSv1.2":
  77. logger.debug("SSL context options, enable TLSv1.2")
  78. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  79. elif entry == "TLSv1.3":
  80. logger.debug("SSL context options, enable TLSv1.3")
  81. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  82. elif entry == "-ALL":
  83. logger.debug("SSL context options, disable ALL")
  84. ssl_context_options |= ssl.OP_NO_SSLv2
  85. ssl_context_options |= ssl.OP_NO_SSLv3
  86. ssl_context_options |= ssl.OP_NO_TLSv1
  87. ssl_context_options |= ssl.OP_NO_TLSv1_1
  88. ssl_context_options |= ssl.OP_NO_TLSv1_2
  89. ssl_context_options |= ssl.OP_NO_TLSv1_3
  90. elif entry == "-SSLv2":
  91. ssl_context_options |= ssl.OP_NO_SSLv2
  92. logger.debug("SSL context options, disable SSLv2")
  93. elif entry == "-SSLv3":
  94. ssl_context_options |= ssl.OP_NO_SSLv3
  95. logger.debug("SSL context options, disable SSLv3")
  96. elif entry == "-TLSv1":
  97. logger.debug("SSL context options, disable TLSv1")
  98. ssl_context_options |= ssl.OP_NO_TLSv1
  99. elif entry == "-TLSv1.1":
  100. logger.debug("SSL context options, disable TLSv1.1")
  101. ssl_context_options |= ssl.OP_NO_TLSv1_1
  102. elif entry == "-TLSv1.2":
  103. logger.debug("SSL context options, disable TLSv1.2")
  104. ssl_context_options |= ssl.OP_NO_TLSv1_2
  105. elif entry == "-TLSv1.3":
  106. logger.debug("SSL context options, disable TLSv1.3")
  107. ssl_context_options |= ssl.OP_NO_TLSv1_3
  108. else:
  109. raise RuntimeError("SSL protocol config contains unsupported entry '%s'" % (entry))
  110. logger.debug("SSL resulting context options: '0x%x'", ssl_context_options)
  111. return ssl_context_options
  112. def ssl_context_minimum_version_by_options(ssl_context_options):
  113. logger.debug("SSL calculate minimum version by context options: '0x%x'", ssl_context_options)
  114. ssl_context_minimum_version = ssl.TLSVersion.SSLv3 # default
  115. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_minimum_version == ssl.TLSVersion.SSLv3)):
  116. ssl_context_minimum_version = ssl.TLSVersion.TLSv1
  117. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1)):
  118. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_1
  119. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_1)):
  120. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_2
  121. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_2)):
  122. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_3
  123. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_3)):
  124. ssl_context_minimum_version = 0 # all disabled
  125. logger.debug("SSL context options: '0x%x' results in minimum version: %s", ssl_context_options, ssl_context_minimum_version)
  126. return ssl_context_minimum_version
  127. def ssl_context_maximum_version_by_options(ssl_context_options):
  128. logger.debug("SSL calculate maximum version by context options: '0x%x'", ssl_context_options)
  129. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_3 # default
  130. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_3)):
  131. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_2
  132. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_2)):
  133. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_1
  134. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_1)):
  135. ssl_context_maximum_version = ssl.TLSVersion.TLSv1
  136. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1)):
  137. ssl_context_maximum_version = ssl.TLSVersion.SSLv3
  138. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_maximum_version == ssl.TLSVersion.SSLv3)):
  139. ssl_context_maximum_version = 0
  140. logger.debug("SSL context options: '0x%x' results in maximum version: %s", ssl_context_options, ssl_context_maximum_version)
  141. return ssl_context_maximum_version
  142. def ssl_get_protocols(context):
  143. protocols = []
  144. if not (context.options & ssl.OP_NO_SSLv3):
  145. if (context.minimum_version < ssl.TLSVersion.TLSv1):
  146. protocols.append("SSLv3")
  147. if not (context.options & ssl.OP_NO_TLSv1):
  148. if (context.minimum_version < ssl.TLSVersion.TLSv1_1) and (context.maximum_version >= ssl.TLSVersion.TLSv1):
  149. protocols.append("TLSv1")
  150. if not (context.options & ssl.OP_NO_TLSv1_1):
  151. if (context.minimum_version < ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_1):
  152. protocols.append("TLSv1.1")
  153. if not (context.options & ssl.OP_NO_TLSv1_2):
  154. if (context.minimum_version <= ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_2):
  155. protocols.append("TLSv1.2")
  156. if not (context.options & ssl.OP_NO_TLSv1_3):
  157. if (context.minimum_version <= ssl.TLSVersion.TLSv1_3) and (context.maximum_version >= ssl.TLSVersion.TLSv1_3):
  158. protocols.append("TLSv1.3")
  159. return protocols