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