Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1 | from six import PY3, binary_type, text_type |
| 2 | |
Jean-Paul Calderone | e36b31a | 2014-01-08 16:55:06 -0500 | [diff] [blame] | 3 | from cryptography.hazmat.bindings.openssl.binding import Binding |
| 4 | binding = Binding() |
| 5 | ffi = binding.ffi |
| 6 | lib = binding.lib |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 7 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 8 | |
| 9 | |
| 10 | def text(charp): |
Jean-Paul Calderone | 130cd0e | 2015-03-15 15:49:33 -0400 | [diff] [blame] | 11 | """ |
| 12 | Get a native string type representing of the given CFFI ``char*`` object. |
| 13 | |
| 14 | :param charp: A C-style string represented using CFFI. |
| 15 | |
| 16 | :return: :class:`str` |
| 17 | """ |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 18 | return native(ffi.string(charp)) |
| 19 | |
| 20 | |
| 21 | |
| 22 | def exception_from_error_queue(exception_type): |
| 23 | """ |
| 24 | Convert an OpenSSL library failure into a Python exception. |
| 25 | |
| 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 | """ |
Jean-Paul Calderone | de07546 | 2014-01-18 10:34:12 -0500 | [diff] [blame] | 31 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 32 | errors = [] |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 33 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 34 | while True: |
| 35 | error = lib.ERR_get_error() |
| 36 | if error == 0: |
| 37 | break |
| 38 | errors.append(( |
Jean-Paul Calderone | de07546 | 2014-01-18 10:34:12 -0500 | [diff] [blame] | 39 | text(lib.ERR_lib_error_string(error)), |
| 40 | text(lib.ERR_func_error_string(error)), |
| 41 | text(lib.ERR_reason_error_string(error)))) |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 42 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 43 | raise exception_type(errors) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 44 | |
| 45 | |
| 46 | |
| 47 | def native(s): |
| 48 | """ |
| 49 | Convert :py:class:`bytes` or :py:class:`unicode` to the native |
Jean-Paul Calderone | aca50f4 | 2014-01-11 14:43:37 -0500 | [diff] [blame] | 50 | :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] | 51 | |
| 52 | :raise UnicodeError: The input string is not UTF-8 decodeable. |
| 53 | |
| 54 | :raise TypeError: The input is neither :py:class:`bytes` nor |
| 55 | :py:class:`unicode`. |
| 56 | """ |
| 57 | if not isinstance(s, (binary_type, text_type)): |
| 58 | raise TypeError("%r is neither bytes nor unicode" % s) |
| 59 | if PY3: |
| 60 | if isinstance(s, binary_type): |
| 61 | return s.decode("utf-8") |
| 62 | else: |
| 63 | if isinstance(s, text_type): |
| 64 | return s.encode("utf-8") |
| 65 | return s |
| 66 | |
| 67 | |
| 68 | |
| 69 | if PY3: |
| 70 | def byte_string(s): |
| 71 | return s.encode("charmap") |
| 72 | else: |
| 73 | def byte_string(s): |
| 74 | return s |