blob: 074ef3d357c64a04ec233df8d34e1c31a4c73ccb [file] [log] [blame]
Jean-Paul Calderone6462b072015-03-29 07:03:11 -04001from warnings import warn
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -04002import sys
3
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
7binding = Binding()
Alex Gaynorda9e4422015-09-05 10:40:29 -04008binding.init_static_locks()
Jean-Paul Calderonee36b31a2014-01-08 16:55:06 -05009ffi = binding.ffi
10lib = binding.lib
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050011
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070012
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070013def text(charp):
Jean-Paul Calderone130cd0e2015-03-15 15:49:33 -040014 """
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 Calderone6d862182015-04-11 07:24:52 -040021 if not charp:
22 return ""
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070023 return native(ffi.string(charp))
24
25
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070026def 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 Calderonede075462014-01-18 10:34:12 -050035
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050036 errors = []
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070037
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050038 while True:
39 error = lib.ERR_get_error()
40 if error == 0:
41 break
42 errors.append((
Alex Gaynorca87ff62015-09-04 23:31:03 -040043 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 Calderonec86bb7d2013-12-29 10:25:59 -050046
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070047 raise exception_type(errors)
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050048
49
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050050def native(s):
51 """
52 Convert :py:class:`bytes` or :py:class:`unicode` to the native
Jean-Paul Calderoneaca50f42014-01-11 14:43:37 -050053 :py:class:`str` type, using UTF-8 encoding if conversion is necessary.
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050054
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 Calderonecbb68cc2015-04-11 12:41:30 -040071def 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 Calderone4f0467a2014-01-11 11:58:41 -050088if PY3:
89 def byte_string(s):
90 return s.encode("charmap")
91else:
92 def byte_string(s):
93 return s
Jean-Paul Calderone00f84eb2015-04-13 12:47:21 -040094
95
96# A marker object to observe whether some optional arguments are passed any
97# value or not.
98UNSPECIFIED = object()
Jean-Paul Calderonef0e74562015-04-13 21:43:33 -040099
Jean-Paul Calderone0c021992015-03-29 07:46:30 -0400100_TEXT_WARNING = (
Jean-Paul Calderone13a0e652015-03-29 07:58:51 -0400101 text_type.__name__ + " for {0} is no longer accepted, use bytes"
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400102)
103
Alex Gaynorca87ff62015-09-04 23:31:03 -0400104
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -0400105def text_to_bytes_and_warn(label, obj):
Jean-Paul Calderone0894b172015-03-29 07:15:14 -0400106 """
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 Calderone6462b072015-03-29 07:03:11 -0400118 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