Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 1 | from warnings import warn |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 2 | import sys |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 3 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 4 | from six import PY3, binary_type, text_type |
| 5 | |
Jean-Paul Calderone | e36b31a | 2014-01-08 16:55:06 -0500 | [diff] [blame] | 6 | from cryptography.hazmat.bindings.openssl.binding import Binding |
| 7 | binding = Binding() |
| 8 | ffi = binding.ffi |
| 9 | lib = binding.lib |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 10 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 11 | |
| 12 | |
| 13 | def text(charp): |
Jean-Paul Calderone | 130cd0e | 2015-03-15 15:49:33 -0400 | [diff] [blame] | 14 | """ |
| 15 | Get a native string type representing of the given CFFI ``char*`` object. |
| 16 | |
| 17 | :param charp: A C-style string represented using CFFI. |
| 18 | |
| 19 | :return: :class:`str` |
| 20 | """ |
Jean-Paul Calderone | 6d86218 | 2015-04-11 07:24:52 -0400 | [diff] [blame] | 21 | if not charp: |
| 22 | return "" |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 23 | return native(ffi.string(charp)) |
| 24 | |
| 25 | |
| 26 | |
| 27 | def exception_from_error_queue(exception_type): |
| 28 | """ |
| 29 | Convert an OpenSSL library failure into a Python exception. |
| 30 | |
| 31 | When a call to the native OpenSSL library fails, this is usually signalled |
| 32 | by the return value, and an error code is stored in an error queue |
| 33 | associated with the current thread. The err library provides functions to |
| 34 | obtain these error codes and textual error messages. |
| 35 | """ |
Jean-Paul Calderone | de07546 | 2014-01-18 10:34:12 -0500 | [diff] [blame] | 36 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 37 | errors = [] |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 38 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 39 | while True: |
| 40 | error = lib.ERR_get_error() |
| 41 | if error == 0: |
| 42 | break |
| 43 | errors.append(( |
Jean-Paul Calderone | de07546 | 2014-01-18 10:34:12 -0500 | [diff] [blame] | 44 | text(lib.ERR_lib_error_string(error)), |
| 45 | text(lib.ERR_func_error_string(error)), |
| 46 | text(lib.ERR_reason_error_string(error)))) |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 47 | |
Stephen Holsapple | 0d9815f | 2014-08-27 19:36:53 -0700 | [diff] [blame] | 48 | raise exception_type(errors) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 49 | |
| 50 | |
| 51 | |
| 52 | def native(s): |
| 53 | """ |
| 54 | Convert :py:class:`bytes` or :py:class:`unicode` to the native |
Jean-Paul Calderone | aca50f4 | 2014-01-11 14:43:37 -0500 | [diff] [blame] | 55 | :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] | 56 | |
| 57 | :raise UnicodeError: The input string is not UTF-8 decodeable. |
| 58 | |
| 59 | :raise TypeError: The input is neither :py:class:`bytes` nor |
| 60 | :py:class:`unicode`. |
| 61 | """ |
| 62 | if not isinstance(s, (binary_type, text_type)): |
| 63 | raise TypeError("%r is neither bytes nor unicode" % s) |
| 64 | if PY3: |
| 65 | if isinstance(s, binary_type): |
| 66 | return s.decode("utf-8") |
| 67 | else: |
| 68 | if isinstance(s, text_type): |
| 69 | return s.encode("utf-8") |
| 70 | return s |
| 71 | |
| 72 | |
| 73 | |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 74 | def path_string(s): |
| 75 | """ |
| 76 | Convert a Python string to a :py:class:`bytes` string identifying the same |
| 77 | path and which can be passed into an OpenSSL API accepting a filename. |
| 78 | |
| 79 | :param s: An instance of :py:class:`bytes` or :py:class:`unicode`. |
| 80 | |
| 81 | :return: An instance of :py:class:`bytes`. |
| 82 | """ |
| 83 | if isinstance(s, binary_type): |
| 84 | return s |
| 85 | elif isinstance(s, text_type): |
| 86 | return s.encode(sys.getfilesystemencoding()) |
| 87 | else: |
| 88 | raise TypeError("Path must be represented as bytes or unicode string") |
| 89 | |
| 90 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 91 | if PY3: |
| 92 | def byte_string(s): |
| 93 | return s.encode("charmap") |
| 94 | else: |
| 95 | def byte_string(s): |
| 96 | return s |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 97 | |
Jean-Paul Calderone | 0c02199 | 2015-03-29 07:46:30 -0400 | [diff] [blame] | 98 | _TEXT_WARNING = ( |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 99 | text_type.__name__ + " for {0} is no longer accepted, use bytes" |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 100 | ) |
| 101 | |
| 102 | def warn_text(label, obj): |
Jean-Paul Calderone | 0894b17 | 2015-03-29 07:15:14 -0400 | [diff] [blame] | 103 | """ |
| 104 | If ``obj`` is text, emit a warning that it should be bytes instead and try |
| 105 | to convert it to bytes automatically. |
| 106 | |
| 107 | :param str label: The name of the parameter from which ``obj`` was taken |
| 108 | (so a developer can easily find the source of the problem and correct |
| 109 | it). |
| 110 | |
| 111 | :return: If ``obj`` is the text string type, a ``bytes`` object giving the |
| 112 | UTF-8 encoding of that text is returned. Otherwise, ``obj`` itself is |
| 113 | returned. |
| 114 | """ |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 115 | if isinstance(obj, text_type): |
| 116 | warn( |
| 117 | _TEXT_WARNING.format(label), |
| 118 | category=DeprecationWarning, |
| 119 | stacklevel=3 |
| 120 | ) |
| 121 | return obj.encode('utf-8') |
| 122 | return obj |