blob: 0cc34d80cdaf161dbf0a8da74f2a9bd5968d362e [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
12
13def 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
26
27def exception_from_error_queue(exception_type):
28 """
29 Convert an OpenSSL library failure into a Python exception.
30
31 When a call to the native OpenSSL library fails, this is usually signalled
32 by the return value, and an error code is stored in an error queue
33 associated with the current thread. The err library provides functions to
34 obtain these error codes and textual error messages.
35 """
Jean-Paul Calderonede075462014-01-18 10:34:12 -050036
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((
Jean-Paul Calderonede075462014-01-18 10:34:12 -050044 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
51
52def native(s):
53 """
54 Convert :py:class:`bytes` or :py:class:`unicode` to the native
Jean-Paul Calderoneaca50f42014-01-11 14:43:37 -050055 :py:class:`str` type, using UTF-8 encoding if conversion is necessary.
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050056
57 :raise UnicodeError: The input string is not UTF-8 decodeable.
58
59 :raise TypeError: The input is neither :py:class:`bytes` nor
60 :py:class:`unicode`.
61 """
62 if not isinstance(s, (binary_type, text_type)):
63 raise TypeError("%r is neither bytes nor unicode" % s)
64 if PY3:
65 if isinstance(s, binary_type):
66 return s.decode("utf-8")
67 else:
68 if isinstance(s, text_type):
69 return s.encode("utf-8")
70 return s
71
72
73
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -040074def path_string(s):
75 """
76 Convert a Python string to a :py:class:`bytes` string identifying the same
77 path and which can be passed into an OpenSSL API accepting a filename.
78
79 :param s: An instance of :py:class:`bytes` or :py:class:`unicode`.
80
81 :return: An instance of :py:class:`bytes`.
82 """
83 if isinstance(s, binary_type):
84 return s
85 elif isinstance(s, text_type):
86 return s.encode(sys.getfilesystemencoding())
87 else:
88 raise TypeError("Path must be represented as bytes or unicode string")
89
90
Jean-Paul Calderone4f0467a2014-01-11 11:58:41 -050091if PY3:
92 def byte_string(s):
93 return s.encode("charmap")
94else:
95 def byte_string(s):
96 return s
Jean-Paul Calderone00f84eb2015-04-13 12:47:21 -040097
98
99# A marker object to observe whether some optional arguments are passed any
100# value or not.
101UNSPECIFIED = object()
Jean-Paul Calderonef0e74562015-04-13 21:43:33 -0400102
Jean-Paul Calderone0c021992015-03-29 07:46:30 -0400103_TEXT_WARNING = (
Jean-Paul Calderone13a0e652015-03-29 07:58:51 -0400104 text_type.__name__ + " for {0} is no longer accepted, use bytes"
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400105)
106
Jean-Paul Calderone39a8d592015-04-13 20:49:50 -0400107def text_to_bytes_and_warn(label, obj):
Jean-Paul Calderone0894b172015-03-29 07:15:14 -0400108 """
109 If ``obj`` is text, emit a warning that it should be bytes instead and try
110 to convert it to bytes automatically.
111
112 :param str label: The name of the parameter from which ``obj`` was taken
113 (so a developer can easily find the source of the problem and correct
114 it).
115
116 :return: If ``obj`` is the text string type, a ``bytes`` object giving the
117 UTF-8 encoding of that text is returned. Otherwise, ``obj`` itself is
118 returned.
119 """
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400120 if isinstance(obj, text_type):
121 warn(
122 _TEXT_WARNING.format(label),
123 category=DeprecationWarning,
124 stacklevel=3
125 )
126 return obj.encode('utf-8')
127 return obj