Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/OpenSSL/_util.py: 50%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import annotations
3import os
4import sys
5import warnings
6from typing import Any, Callable, NoReturn, Union
8from cryptography.hazmat.bindings.openssl.binding import Binding
10if sys.version_info >= (3, 9):
11 StrOrBytesPath = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]
12else:
13 StrOrBytesPath = Union[str, bytes, os.PathLike]
15binding = Binding()
16ffi = binding.ffi
17lib = binding.lib
20# This is a special CFFI allocator that does not bother to zero its memory
21# after allocation. This has vastly better performance on large allocations and
22# so should be used whenever we don't need the memory zeroed out.
23no_zero_allocator = ffi.new_allocator(should_clear_after_alloc=False)
26def text(charp: Any) -> str:
27 """
28 Get a native string type representing of the given CFFI ``char*`` object.
30 :param charp: A C-style string represented using CFFI.
32 :return: :class:`str`
33 """
34 if not charp:
35 return ""
36 return ffi.string(charp).decode("utf-8")
39def exception_from_error_queue(exception_type: type[Exception]) -> NoReturn:
40 """
41 Convert an OpenSSL library failure into a Python exception.
43 When a call to the native OpenSSL library fails, this is usually signalled
44 by the return value, and an error code is stored in an error queue
45 associated with the current thread. The err library provides functions to
46 obtain these error codes and textual error messages.
47 """
48 errors = []
50 while True:
51 error = lib.ERR_get_error()
52 if error == 0:
53 break
54 errors.append(
55 (
56 text(lib.ERR_lib_error_string(error)),
57 text(lib.ERR_func_error_string(error)),
58 text(lib.ERR_reason_error_string(error)),
59 )
60 )
62 raise exception_type(errors)
65def make_assert(error: type[Exception]) -> Callable[[bool], Any]:
66 """
67 Create an assert function that uses :func:`exception_from_error_queue` to
68 raise an exception wrapped by *error*.
69 """
71 def openssl_assert(ok: bool) -> None:
72 """
73 If *ok* is not True, retrieve the error from OpenSSL and raise it.
74 """
75 if ok is not True:
76 exception_from_error_queue(error)
78 return openssl_assert
81def path_bytes(s: StrOrBytesPath) -> bytes:
82 """
83 Convert a Python path to a :py:class:`bytes` for the path which can be
84 passed into an OpenSSL API accepting a filename.
86 :param s: A path (valid for os.fspath).
88 :return: An instance of :py:class:`bytes`.
89 """
90 b = os.fspath(s)
92 if isinstance(b, str):
93 return b.encode(sys.getfilesystemencoding())
94 else:
95 return b
98def byte_string(s: str) -> bytes:
99 return s.encode("charmap")
102# A marker object to observe whether some optional arguments are passed any
103# value or not.
104UNSPECIFIED = object()
106_TEXT_WARNING = "str for {0} is no longer accepted, use bytes"
109def text_to_bytes_and_warn(label: str, obj: Any) -> Any:
110 """
111 If ``obj`` is text, emit a warning that it should be bytes instead and try
112 to convert it to bytes automatically.
114 :param str label: The name of the parameter from which ``obj`` was taken
115 (so a developer can easily find the source of the problem and correct
116 it).
118 :return: If ``obj`` is the text string type, a ``bytes`` object giving the
119 UTF-8 encoding of that text is returned. Otherwise, ``obj`` itself is
120 returned.
121 """
122 if isinstance(obj, str):
123 warnings.warn(
124 _TEXT_WARNING.format(label),
125 category=DeprecationWarning,
126 stacklevel=3,
127 )
128 return obj.encode("utf-8")
129 return obj