Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 1 | import sys |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 2 | import warnings |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 3 | |
Hugo van Kemenade | 60827f8 | 2019-08-30 00:39:35 +0300 | [diff] [blame] | 4 | from six import PY2, binary_type, text_type |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 5 | |
Jean-Paul Calderone | e36b31a | 2014-01-08 16:55:06 -0500 | [diff] [blame] | 6 | from cryptography.hazmat.bindings.openssl.binding import Binding |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 7 | |
| 8 | |
Jean-Paul Calderone | e36b31a | 2014-01-08 16:55:06 -0500 | [diff] [blame] | 9 | binding = Binding() |
Alex Gaynor | da9e442 | 2015-09-05 10:40:29 -0400 | [diff] [blame] | 10 | binding.init_static_locks() |
Jean-Paul Calderone | e36b31a | 2014-01-08 16:55:06 -0500 | [diff] [blame] | 11 | ffi = binding.ffi |
| 12 | lib = binding.lib |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 13 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 14 | |
Cory Benfield | e62840e | 2016-11-28 12:17:08 +0000 | [diff] [blame] | 15 | # This is a special CFFI allocator that does not bother to zero its memory |
| 16 | # after allocation. This has vastly better performance on large allocations and |
| 17 | # so should be used whenever we don't need the memory zeroed out. |
| 18 | no_zero_allocator = ffi.new_allocator(should_clear_after_alloc=False) |
| 19 | |
| 20 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 21 | def text(charp): |
Jean-Paul Calderone | 130cd0e | 2015-03-15 15:49:33 -0400 | [diff] [blame] | 22 | """ |
| 23 | Get a native string type representing of the given CFFI ``char*`` object. |
| 24 | |
| 25 | :param charp: A C-style string represented using CFFI. |
| 26 | |
| 27 | :return: :class:`str` |
| 28 | """ |
Jean-Paul Calderone | 6d86218 | 2015-04-11 07:24:52 -0400 | [diff] [blame] | 29 | if not charp: |
| 30 | return "" |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 31 | return native(ffi.string(charp)) |
| 32 | |
| 33 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 34 | def exception_from_error_queue(exception_type): |
| 35 | """ |
| 36 | Convert an OpenSSL library failure into a Python exception. |
| 37 | |
| 38 | When a call to the native OpenSSL library fails, this is usually signalled |
| 39 | by the return value, and an error code is stored in an error queue |
| 40 | associated with the current thread. The err library provides functions to |
| 41 | obtain these error codes and textual error messages. |
| 42 | """ |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 43 | errors = [] |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 44 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 45 | while True: |
| 46 | error = lib.ERR_get_error() |
| 47 | if error == 0: |
| 48 | break |
| 49 | errors.append(( |
Alex Gaynor | ca87ff6 | 2015-09-04 23:31:03 -0400 | [diff] [blame] | 50 | text(lib.ERR_lib_error_string(error)), |
| 51 | text(lib.ERR_func_error_string(error)), |
| 52 | text(lib.ERR_reason_error_string(error)))) |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 53 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 54 | raise exception_type(errors) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 55 | |
| 56 | |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 57 | def make_assert(error): |
| 58 | """ |
| 59 | Create an assert function that uses :func:`exception_from_error_queue` to |
| 60 | raise an exception wrapped by *error*. |
| 61 | """ |
| 62 | def openssl_assert(ok): |
| 63 | """ |
Hynek Schlawack | aa86121 | 2016-03-13 13:53:48 +0100 | [diff] [blame] | 64 | If *ok* is not True, retrieve the error from OpenSSL and raise it. |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 65 | """ |
Hynek Schlawack | ec47679 | 2016-03-11 15:04:47 +0100 | [diff] [blame] | 66 | if ok is not True: |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 67 | exception_from_error_queue(error) |
| 68 | |
| 69 | return openssl_assert |
| 70 | |
| 71 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 72 | def native(s): |
| 73 | """ |
| 74 | Convert :py:class:`bytes` or :py:class:`unicode` to the native |
Jean-Paul Calderone | aca50f4 | 2014-01-11 14:43:37 -0500 | [diff] [blame] | 75 | :py:class:`str` type, using UTF-8 encoding if conversion is necessary. |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 76 | |
| 77 | :raise UnicodeError: The input string is not UTF-8 decodeable. |
| 78 | |
| 79 | :raise TypeError: The input is neither :py:class:`bytes` nor |
| 80 | :py:class:`unicode`. |
| 81 | """ |
| 82 | if not isinstance(s, (binary_type, text_type)): |
| 83 | raise TypeError("%r is neither bytes nor unicode" % s) |
Hugo van Kemenade | 60827f8 | 2019-08-30 00:39:35 +0300 | [diff] [blame] | 84 | if PY2: |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 85 | if isinstance(s, text_type): |
| 86 | return s.encode("utf-8") |
Hugo van Kemenade | 60827f8 | 2019-08-30 00:39:35 +0300 | [diff] [blame] | 87 | else: |
| 88 | if isinstance(s, binary_type): |
| 89 | return s.decode("utf-8") |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 90 | return s |
| 91 | |
| 92 | |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 93 | def path_string(s): |
| 94 | """ |
| 95 | Convert a Python string to a :py:class:`bytes` string identifying the same |
| 96 | path and which can be passed into an OpenSSL API accepting a filename. |
| 97 | |
| 98 | :param s: An instance of :py:class:`bytes` or :py:class:`unicode`. |
| 99 | |
| 100 | :return: An instance of :py:class:`bytes`. |
| 101 | """ |
| 102 | if isinstance(s, binary_type): |
| 103 | return s |
| 104 | elif isinstance(s, text_type): |
| 105 | return s.encode(sys.getfilesystemencoding()) |
| 106 | else: |
| 107 | raise TypeError("Path must be represented as bytes or unicode string") |
| 108 | |
| 109 | |
Hugo van Kemenade | 60827f8 | 2019-08-30 00:39:35 +0300 | [diff] [blame] | 110 | if PY2: |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 111 | def byte_string(s): |
| 112 | return s |
Hugo van Kemenade | 60827f8 | 2019-08-30 00:39:35 +0300 | [diff] [blame] | 113 | else: |
| 114 | def byte_string(s): |
| 115 | return s.encode("charmap") |
Jean-Paul Calderone | 00f84eb | 2015-04-13 12:47:21 -0400 | [diff] [blame] | 116 | |
| 117 | |
| 118 | # A marker object to observe whether some optional arguments are passed any |
| 119 | # value or not. |
| 120 | UNSPECIFIED = object() |
Jean-Paul Calderone | f0e7456 | 2015-04-13 21:43:33 -0400 | [diff] [blame] | 121 | |
Jean-Paul Calderone | 0c02199 | 2015-03-29 07:46:30 -0400 | [diff] [blame] | 122 | _TEXT_WARNING = ( |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 123 | text_type.__name__ + " for {0} is no longer accepted, use bytes" |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 124 | ) |
| 125 | |
Alex Gaynor | ca87ff6 | 2015-09-04 23:31:03 -0400 | [diff] [blame] | 126 | |
Jean-Paul Calderone | 39a8d59 | 2015-04-13 20:49:50 -0400 | [diff] [blame] | 127 | def text_to_bytes_and_warn(label, obj): |
Jean-Paul Calderone | 0894b17 | 2015-03-29 07:15:14 -0400 | [diff] [blame] | 128 | """ |
| 129 | If ``obj`` is text, emit a warning that it should be bytes instead and try |
| 130 | to convert it to bytes automatically. |
| 131 | |
| 132 | :param str label: The name of the parameter from which ``obj`` was taken |
| 133 | (so a developer can easily find the source of the problem and correct |
| 134 | it). |
| 135 | |
| 136 | :return: If ``obj`` is the text string type, a ``bytes`` object giving the |
| 137 | UTF-8 encoding of that text is returned. Otherwise, ``obj`` itself is |
| 138 | returned. |
| 139 | """ |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 140 | if isinstance(obj, text_type): |
Hynek Schlawack | f90e368 | 2016-03-11 11:21:13 +0100 | [diff] [blame] | 141 | warnings.warn( |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 142 | _TEXT_WARNING.format(label), |
| 143 | category=DeprecationWarning, |
| 144 | stacklevel=3 |
| 145 | ) |
| 146 | return obj.encode('utf-8') |
| 147 | return obj |
Daniel Holth | 079c963 | 2019-11-17 22:45:52 -0500 | [diff] [blame^] | 148 | |
| 149 | |
| 150 | try: |
| 151 | # newer versions of cffi free the buffer deterministically |
| 152 | with ffi.from_buffer(b""): |
| 153 | pass |
| 154 | from_buffer = ffi.from_buffer |
| 155 | except AttributeError: |
| 156 | # cffi < 0.12 frees the buffer with refcounting gc |
| 157 | from contextlib import contextmanager |
| 158 | |
| 159 | @contextmanager |
| 160 | def from_buffer(*args): |
| 161 | yield ffi.from_buffer(*args) |