blob: a4b29e3a7e856da91caefc64d70cb4a9586d0db5 [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()
8ffi = binding.ffi
9lib = binding.lib
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050010
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070011
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070012def text(charp):
Jean-Paul Calderone130cd0e2015-03-15 15:49:33 -040013 """
14 Get a native string type representing of the given CFFI ``char*`` object.
15
16 :param charp: A C-style string represented using CFFI.
17
18 :return: :class:`str`
19 """
Jean-Paul Calderone6d862182015-04-11 07:24:52 -040020 if not charp:
21 return ""
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070022 return native(ffi.string(charp))
23
24
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070025def exception_from_error_queue(exception_type):
26 """
27 Convert an OpenSSL library failure into a Python exception.
28
29 When a call to the native OpenSSL library fails, this is usually signalled
30 by the return value, and an error code is stored in an error queue
31 associated with the current thread. The err library provides functions to
32 obtain these error codes and textual error messages.
33 """
Jean-Paul Calderonede075462014-01-18 10:34:12 -050034
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050035 errors = []
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070036
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050037 while True:
38 error = lib.ERR_get_error()
39 if error == 0:
40 break
41 errors.append((
Alex Gaynorca87ff62015-09-04 23:31:03 -040042 text(lib.ERR_lib_error_string(error)),
43 text(lib.ERR_func_error_string(error)),
44 text(lib.ERR_reason_error_string(error))))
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050045
Stephen Holsapple0d9815f2014-08-27 19:36:53 -070046 raise exception_type(errors)
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050047
48
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050049def native(s):
50 """
51 Convert :py:class:`bytes` or :py:class:`unicode` to the native
Jean-Paul Calderoneaca50f42014-01-11 14:43:37 -050052 :py:class:`str` type, using UTF-8 encoding if conversion is necessary.
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050053
54 :raise UnicodeError: The input string is not UTF-8 decodeable.
55
56 :raise TypeError: The input is neither :py:class:`bytes` nor
57 :py:class:`unicode`.
58 """
59 if not isinstance(s, (binary_type, text_type)):
60 raise TypeError("%r is neither bytes nor unicode" % s)
61 if PY3:
62 if isinstance(s, binary_type):
63 return s.decode("utf-8")
64 else:
65 if isinstance(s, text_type):
66 return s.encode("utf-8")
67 return s
68
69
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -040070def path_string(s):
71 """
72 Convert a Python string to a :py:class:`bytes` string identifying the same
73 path and which can be passed into an OpenSSL API accepting a filename.
74
75 :param s: An instance of :py:class:`bytes` or :py:class:`unicode`.
76
77 :return: An instance of :py:class:`bytes`.
78 """
79 if isinstance(s, binary_type):
80 return s
81 elif isinstance(s, text_type):
82 return s.encode(sys.getfilesystemencoding())
83 else:
84 raise TypeError("Path must be represented as bytes or unicode string")
85
86
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050087if PY3:
88 def byte_string(s):
89 return s.encode("charmap")
90else:
91 def byte_string(s):
92 return s
Jean-Paul Calderone00f84eb2015-04-13 12:47:21 -040093
94
95# A marker object to observe whether some optional arguments are passed any
96# value or not.
97UNSPECIFIED = object()
Jean-Paul Calderonef0e74562015-04-13 21:43:33 -040098
Jean-Paul Calderone0c021992015-03-29 07:46:30 -040099_TEXT_WARNING = (
Jean-Paul Calderone13a0e652015-03-29 07:58:51 -0400100 text_type.__name__ + " for {0} is no longer accepted, use bytes"
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400101)
102
Alex Gaynorca87ff62015-09-04 23:31:03 -0400103
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -0400104def text_to_bytes_and_warn(label, obj):
Jean-Paul Calderone0894b172015-03-29 07:15:14 -0400105 """
106 If ``obj`` is text, emit a warning that it should be bytes instead and try
107 to convert it to bytes automatically.
108
109 :param str label: The name of the parameter from which ``obj`` was taken
110 (so a developer can easily find the source of the problem and correct
111 it).
112
113 :return: If ``obj`` is the text string type, a ``bytes`` object giving the
114 UTF-8 encoding of that text is returned. Otherwise, ``obj`` itself is
115 returned.
116 """
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400117 if isinstance(obj, text_type):
118 warn(
119 _TEXT_WARNING.format(label),
120 category=DeprecationWarning,
121 stacklevel=3
122 )
123 return obj.encode('utf-8')
124 return obj