blob: de292bebf0f3d293a44fa4b3155b38ddd378a094 [file] [log] [blame]
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -05001from six import PY3, binary_type, text_type
2
Jean-Paul Calderonee36b31a2014-01-08 16:55:06 -05003from cryptography.hazmat.bindings.openssl.binding import Binding
4binding = Binding()
5ffi = binding.ffi
6lib = binding.lib
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -05007
Stephen Holsapple0d9815f2014-08-27 19:36:53 -07008
9
10def text(charp):
Jean-Paul Calderone130cd0e2015-03-15 15:49:33 -040011 """
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 Holsapple0d9815f2014-08-27 19:36:53 -070018 return native(ffi.string(charp))
19
20
21
22def 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 Calderonede075462014-01-18 10:34:12 -050031
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050032 errors = []
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070033
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050034 while True:
35 error = lib.ERR_get_error()
36 if error == 0:
37 break
38 errors.append((
Jean-Paul Calderonede075462014-01-18 10:34:12 -050039 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 Calderonec86bb7d2013-12-29 10:25:59 -050042
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070043 raise exception_type(errors)
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050044
45
46
47def native(s):
48 """
49 Convert :py:class:`bytes` or :py:class:`unicode` to the native
Jean-Paul Calderoneaca50f42014-01-11 14:43:37 -050050 :py:class:`str` type, using UTF-8 encoding if conversion is necessary.
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050051
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
69if PY3:
70 def byte_string(s):
71 return s.encode("charmap")
72else:
73 def byte_string(s):
74 return s