blob: 48bcbf56175e76f1e6e40f33dcfdf322c9a7849f [file] [log] [blame]
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -04001import sys
Hynek Schlawackf90e3682016-03-11 11:21:13 +01002import warnings
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -04003
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05004from six import PY3, binary_type, text_type
5
Jean-Paul Calderonee36b31a2014-01-08 16:55:06 -05006from cryptography.hazmat.bindings.openssl.binding import Binding
Hynek Schlawackf90e3682016-03-11 11:21:13 +01007
8
Jean-Paul Calderonee36b31a2014-01-08 16:55:06 -05009binding = Binding()
Alex Gaynorda9e4422015-09-05 10:40:29 -040010binding.init_static_locks()
Jean-Paul Calderonee36b31a2014-01-08 16:55:06 -050011ffi = binding.ffi
12lib = binding.lib
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050013
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070014
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070015def text(charp):
Jean-Paul Calderone130cd0e2015-03-15 15:49:33 -040016 """
17 Get a native string type representing of the given CFFI ``char*`` object.
18
19 :param charp: A C-style string represented using CFFI.
20
21 :return: :class:`str`
22 """
Jean-Paul Calderone6d862182015-04-11 07:24:52 -040023 if not charp:
24 return ""
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070025 return native(ffi.string(charp))
26
27
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070028def exception_from_error_queue(exception_type):
29 """
30 Convert an OpenSSL library failure into a Python exception.
31
32 When a call to the native OpenSSL library fails, this is usually signalled
33 by the return value, and an error code is stored in an error queue
34 associated with the current thread. The err library provides functions to
35 obtain these error codes and textual error messages.
36 """
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050037 errors = []
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070038
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050039 while True:
40 error = lib.ERR_get_error()
41 if error == 0:
42 break
43 errors.append((
Alex Gaynorca87ff62015-09-04 23:31:03 -040044 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 Calderonec86bb7d2013-12-29 10:25:59 -050047
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070048 raise exception_type(errors)
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050049
50
Hynek Schlawackf90e3682016-03-11 11:21:13 +010051def make_assert(error):
52 """
53 Create an assert function that uses :func:`exception_from_error_queue` to
54 raise an exception wrapped by *error*.
55 """
56 def openssl_assert(ok):
57 """
Hynek Schlawackaa861212016-03-13 13:53:48 +010058 If *ok* is not True, retrieve the error from OpenSSL and raise it.
Hynek Schlawackf90e3682016-03-11 11:21:13 +010059 """
Hynek Schlawackec476792016-03-11 15:04:47 +010060 if ok is not True:
Hynek Schlawackf90e3682016-03-11 11:21:13 +010061 exception_from_error_queue(error)
62
63 return openssl_assert
64
65
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050066def native(s):
67 """
68 Convert :py:class:`bytes` or :py:class:`unicode` to the native
Jean-Paul Calderoneaca50f42014-01-11 14:43:37 -050069 :py:class:`str` type, using UTF-8 encoding if conversion is necessary.
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050070
71 :raise UnicodeError: The input string is not UTF-8 decodeable.
72
73 :raise TypeError: The input is neither :py:class:`bytes` nor
74 :py:class:`unicode`.
75 """
76 if not isinstance(s, (binary_type, text_type)):
77 raise TypeError("%r is neither bytes nor unicode" % s)
78 if PY3:
79 if isinstance(s, binary_type):
80 return s.decode("utf-8")
81 else:
82 if isinstance(s, text_type):
83 return s.encode("utf-8")
84 return s
85
86
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -040087def path_string(s):
88 """
89 Convert a Python string to a :py:class:`bytes` string identifying the same
90 path and which can be passed into an OpenSSL API accepting a filename.
91
92 :param s: An instance of :py:class:`bytes` or :py:class:`unicode`.
93
94 :return: An instance of :py:class:`bytes`.
95 """
96 if isinstance(s, binary_type):
97 return s
98 elif isinstance(s, text_type):
99 return s.encode(sys.getfilesystemencoding())
100 else:
101 raise TypeError("Path must be represented as bytes or unicode string")
102
103
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500104if PY3:
105 def byte_string(s):
106 return s.encode("charmap")
107else:
108 def byte_string(s):
109 return s
Jean-Paul Calderone00f84eb2015-04-13 12:47:21 -0400110
111
112# A marker object to observe whether some optional arguments are passed any
113# value or not.
114UNSPECIFIED = object()
Jean-Paul Calderonef0e74562015-04-13 21:43:33 -0400115
Jean-Paul Calderone0c021992015-03-29 07:46:30 -0400116_TEXT_WARNING = (
Jean-Paul Calderone13a0e652015-03-29 07:58:51 -0400117 text_type.__name__ + " for {0} is no longer accepted, use bytes"
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400118)
119
Alex Gaynorca87ff62015-09-04 23:31:03 -0400120
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -0400121def text_to_bytes_and_warn(label, obj):
Jean-Paul Calderone0894b172015-03-29 07:15:14 -0400122 """
123 If ``obj`` is text, emit a warning that it should be bytes instead and try
124 to convert it to bytes automatically.
125
126 :param str label: The name of the parameter from which ``obj`` was taken
127 (so a developer can easily find the source of the problem and correct
128 it).
129
130 :return: If ``obj`` is the text string type, a ``bytes`` object giving the
131 UTF-8 encoding of that text is returned. Otherwise, ``obj`` itself is
132 returned.
133 """
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400134 if isinstance(obj, text_type):
Hynek Schlawackf90e3682016-03-11 11:21:13 +0100135 warnings.warn(
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400136 _TEXT_WARNING.format(label),
137 category=DeprecationWarning,
138 stacklevel=3
139 )
140 return obj.encode('utf-8')
141 return obj