blob: cba72adbfa98744b1fb6600f782823d2a4ea56b9 [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 Calderonede075462014-01-18 10:34:12 -050037
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050038 errors = []
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070039
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050040 while True:
41 error = lib.ERR_get_error()
42 if error == 0:
43 break
44 errors.append((
Alex Gaynorca87ff62015-09-04 23:31:03 -040045 text(lib.ERR_lib_error_string(error)),
46 text(lib.ERR_func_error_string(error)),
47 text(lib.ERR_reason_error_string(error))))
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050048
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070049 raise exception_type(errors)
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050050
51
Hynek Schlawackf90e3682016-03-11 11:21:13 +010052def make_assert(error):
53 """
54 Create an assert function that uses :func:`exception_from_error_queue` to
55 raise an exception wrapped by *error*.
56 """
57 def openssl_assert(ok):
58 """
59 If ok is not true-ish, retrieve the error from OpenSSL and raise it.
60 """
61 if not ok:
62 exception_from_error_queue(error)
63
64 return openssl_assert
65
66
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050067def native(s):
68 """
69 Convert :py:class:`bytes` or :py:class:`unicode` to the native
Jean-Paul Calderoneaca50f42014-01-11 14:43:37 -050070 :py:class:`str` type, using UTF-8 encoding if conversion is necessary.
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050071
72 :raise UnicodeError: The input string is not UTF-8 decodeable.
73
74 :raise TypeError: The input is neither :py:class:`bytes` nor
75 :py:class:`unicode`.
76 """
77 if not isinstance(s, (binary_type, text_type)):
78 raise TypeError("%r is neither bytes nor unicode" % s)
79 if PY3:
80 if isinstance(s, binary_type):
81 return s.decode("utf-8")
82 else:
83 if isinstance(s, text_type):
84 return s.encode("utf-8")
85 return s
86
87
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -040088def path_string(s):
89 """
90 Convert a Python string to a :py:class:`bytes` string identifying the same
91 path and which can be passed into an OpenSSL API accepting a filename.
92
93 :param s: An instance of :py:class:`bytes` or :py:class:`unicode`.
94
95 :return: An instance of :py:class:`bytes`.
96 """
97 if isinstance(s, binary_type):
98 return s
99 elif isinstance(s, text_type):
100 return s.encode(sys.getfilesystemencoding())
101 else:
102 raise TypeError("Path must be represented as bytes or unicode string")
103
104
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -0500105if PY3:
106 def byte_string(s):
107 return s.encode("charmap")
108else:
109 def byte_string(s):
110 return s
Jean-Paul Calderone00f84eb2015-04-13 12:47:21 -0400111
112
113# A marker object to observe whether some optional arguments are passed any
114# value or not.
115UNSPECIFIED = object()
Jean-Paul Calderonef0e74562015-04-13 21:43:33 -0400116
Jean-Paul Calderone0c021992015-03-29 07:46:30 -0400117_TEXT_WARNING = (
Jean-Paul Calderone13a0e652015-03-29 07:58:51 -0400118 text_type.__name__ + " for {0} is no longer accepted, use bytes"
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400119)
120
Alex Gaynorca87ff62015-09-04 23:31:03 -0400121
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -0400122def text_to_bytes_and_warn(label, obj):
Jean-Paul Calderone0894b172015-03-29 07:15:14 -0400123 """
124 If ``obj`` is text, emit a warning that it should be bytes instead and try
125 to convert it to bytes automatically.
126
127 :param str label: The name of the parameter from which ``obj`` was taken
128 (so a developer can easily find the source of the problem and correct
129 it).
130
131 :return: If ``obj`` is the text string type, a ``bytes`` object giving the
132 UTF-8 encoding of that text is returned. Otherwise, ``obj`` itself is
133 returned.
134 """
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400135 if isinstance(obj, text_type):
Hynek Schlawackf90e3682016-03-11 11:21:13 +0100136 warnings.warn(
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400137 _TEXT_WARNING.format(label),
138 category=DeprecationWarning,
139 stacklevel=3
140 )
141 return obj.encode('utf-8')
142 return obj