securetransport.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. # SPDX-License-Identifier: MIT
  2. """
  3. SecureTranport support for urllib3 via ctypes.
  4. This makes platform-native TLS available to urllib3 users on macOS without the
  5. use of a compiler. This is an important feature because the Python Package
  6. Index is moving to become a TLSv1.2-or-higher server, and the default OpenSSL
  7. that ships with macOS is not capable of doing TLSv1.2. The only way to resolve
  8. this is to give macOS users an alternative solution to the problem, and that
  9. solution is to use SecureTransport.
  10. We use ctypes here because this solution must not require a compiler. That's
  11. because pip is not allowed to require a compiler either.
  12. This is not intended to be a seriously long-term solution to this problem.
  13. The hope is that PEP 543 will eventually solve this issue for us, at which
  14. point we can retire this contrib module. But in the short term, we need to
  15. solve the impending tire fire that is Python on Mac without this kind of
  16. contrib module. So...here we are.
  17. To use this module, simply import and inject it::
  18. import urllib3.contrib.securetransport
  19. urllib3.contrib.securetransport.inject_into_urllib3()
  20. Happy TLSing!
  21. """
  22. from __future__ import absolute_import
  23. import contextlib
  24. import ctypes
  25. import errno
  26. import os.path
  27. import shutil
  28. import socket
  29. import ssl
  30. import threading
  31. import weakref
  32. from .. import util
  33. from ._securetransport.bindings import (
  34. Security, SecurityConst, CoreFoundation
  35. )
  36. from ._securetransport.low_level import (
  37. _assert_no_error, _cert_array_from_pem, _temporary_keychain,
  38. _load_client_cert_chain
  39. )
  40. try: # Platform-specific: Python 2
  41. from socket import _fileobject
  42. except ImportError: # Platform-specific: Python 3
  43. _fileobject = None
  44. from ..packages.backports.makefile import backport_makefile
  45. try:
  46. memoryview(b'')
  47. except NameError:
  48. raise ImportError("SecureTransport only works on Pythons with memoryview")
  49. __all__ = ['inject_into_urllib3', 'extract_from_urllib3']
  50. # SNI always works
  51. HAS_SNI = True
  52. orig_util_HAS_SNI = util.HAS_SNI
  53. orig_util_SSLContext = util.ssl_.SSLContext
  54. # This dictionary is used by the read callback to obtain a handle to the
  55. # calling wrapped socket. This is a pretty silly approach, but for now it'll
  56. # do. I feel like I should be able to smuggle a handle to the wrapped socket
  57. # directly in the SSLConnectionRef, but for now this approach will work I
  58. # guess.
  59. #
  60. # We need to lock around this structure for inserts, but we don't do it for
  61. # reads/writes in the callbacks. The reasoning here goes as follows:
  62. #
  63. # 1. It is not possible to call into the callbacks before the dictionary is
  64. # populated, so once in the callback the id must be in the dictionary.
  65. # 2. The callbacks don't mutate the dictionary, they only read from it, and
  66. # so cannot conflict with any of the insertions.
  67. #
  68. # This is good: if we had to lock in the callbacks we'd drastically slow down
  69. # the performance of this code.
  70. _connection_refs = weakref.WeakValueDictionary()
  71. _connection_ref_lock = threading.Lock()
  72. # Limit writes to 16kB. This is OpenSSL's limit, but we'll cargo-cult it over
  73. # for no better reason than we need *a* limit, and this one is right there.
  74. SSL_WRITE_BLOCKSIZE = 16384
  75. # This is our equivalent of util.ssl_.DEFAULT_CIPHERS, but expanded out to
  76. # individual cipher suites. We need to do this becuase this is how
  77. # SecureTransport wants them.
  78. CIPHER_SUITES = [
  79. SecurityConst.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  80. SecurityConst.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  81. SecurityConst.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  82. SecurityConst.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  83. SecurityConst.TLS_DHE_DSS_WITH_AES_256_GCM_SHA384,
  84. SecurityConst.TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
  85. SecurityConst.TLS_DHE_DSS_WITH_AES_128_GCM_SHA256,
  86. SecurityConst.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
  87. SecurityConst.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
  88. SecurityConst.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
  89. SecurityConst.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  90. SecurityConst.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  91. SecurityConst.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
  92. SecurityConst.TLS_DHE_DSS_WITH_AES_256_CBC_SHA256,
  93. SecurityConst.TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
  94. SecurityConst.TLS_DHE_DSS_WITH_AES_256_CBC_SHA,
  95. SecurityConst.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
  96. SecurityConst.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  97. SecurityConst.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  98. SecurityConst.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  99. SecurityConst.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
  100. SecurityConst.TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,
  101. SecurityConst.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
  102. SecurityConst.TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
  103. SecurityConst.TLS_RSA_WITH_AES_256_GCM_SHA384,
  104. SecurityConst.TLS_RSA_WITH_AES_128_GCM_SHA256,
  105. SecurityConst.TLS_RSA_WITH_AES_256_CBC_SHA256,
  106. SecurityConst.TLS_RSA_WITH_AES_128_CBC_SHA256,
  107. SecurityConst.TLS_RSA_WITH_AES_256_CBC_SHA,
  108. SecurityConst.TLS_RSA_WITH_AES_128_CBC_SHA,
  109. ]
  110. # Basically this is simple: for PROTOCOL_SSLv23 we turn it into a low of
  111. # TLSv1 and a high of TLSv1.2. For everything else, we pin to that version.
  112. _protocol_to_min_max = {
  113. ssl.PROTOCOL_SSLv23: (SecurityConst.kTLSProtocol1, SecurityConst.kTLSProtocol12),
  114. }
  115. if hasattr(ssl, "PROTOCOL_SSLv2"):
  116. _protocol_to_min_max[ssl.PROTOCOL_SSLv2] = (
  117. SecurityConst.kSSLProtocol2, SecurityConst.kSSLProtocol2
  118. )
  119. if hasattr(ssl, "PROTOCOL_SSLv3"):
  120. _protocol_to_min_max[ssl.PROTOCOL_SSLv3] = (
  121. SecurityConst.kSSLProtocol3, SecurityConst.kSSLProtocol3
  122. )
  123. if hasattr(ssl, "PROTOCOL_TLSv1"):
  124. _protocol_to_min_max[ssl.PROTOCOL_TLSv1] = (
  125. SecurityConst.kTLSProtocol1, SecurityConst.kTLSProtocol1
  126. )
  127. if hasattr(ssl, "PROTOCOL_TLSv1_1"):
  128. _protocol_to_min_max[ssl.PROTOCOL_TLSv1_1] = (
  129. SecurityConst.kTLSProtocol11, SecurityConst.kTLSProtocol11
  130. )
  131. if hasattr(ssl, "PROTOCOL_TLSv1_2"):
  132. _protocol_to_min_max[ssl.PROTOCOL_TLSv1_2] = (
  133. SecurityConst.kTLSProtocol12, SecurityConst.kTLSProtocol12
  134. )
  135. if hasattr(ssl, "PROTOCOL_TLS"):
  136. _protocol_to_min_max[ssl.PROTOCOL_TLS] = _protocol_to_min_max[ssl.PROTOCOL_SSLv23]
  137. def inject_into_urllib3():
  138. """
  139. Monkey-patch urllib3 with SecureTransport-backed SSL-support.
  140. """
  141. util.ssl_.SSLContext = SecureTransportContext
  142. util.HAS_SNI = HAS_SNI
  143. util.ssl_.HAS_SNI = HAS_SNI
  144. util.IS_SECURETRANSPORT = True
  145. util.ssl_.IS_SECURETRANSPORT = True
  146. def extract_from_urllib3():
  147. """
  148. Undo monkey-patching by :func:`inject_into_urllib3`.
  149. """
  150. util.ssl_.SSLContext = orig_util_SSLContext
  151. util.HAS_SNI = orig_util_HAS_SNI
  152. util.ssl_.HAS_SNI = orig_util_HAS_SNI
  153. util.IS_SECURETRANSPORT = False
  154. util.ssl_.IS_SECURETRANSPORT = False
  155. def _read_callback(connection_id, data_buffer, data_length_pointer):
  156. """
  157. SecureTransport read callback. This is called by ST to request that data
  158. be returned from the socket.
  159. """
  160. wrapped_socket = None
  161. try:
  162. wrapped_socket = _connection_refs.get(connection_id)
  163. if wrapped_socket is None:
  164. return SecurityConst.errSSLInternal
  165. base_socket = wrapped_socket.socket
  166. requested_length = data_length_pointer[0]
  167. timeout = wrapped_socket.gettimeout()
  168. error = None
  169. read_count = 0
  170. buffer = (ctypes.c_char * requested_length).from_address(data_buffer)
  171. buffer_view = memoryview(buffer)
  172. try:
  173. while read_count < requested_length:
  174. if timeout is None or timeout >= 0:
  175. readables = util.wait_for_read([base_socket], timeout)
  176. if not readables:
  177. raise socket.error(errno.EAGAIN, 'timed out')
  178. # We need to tell ctypes that we have a buffer that can be
  179. # written to. Upsettingly, we do that like this:
  180. chunk_size = base_socket.recv_into(
  181. buffer_view[read_count:requested_length]
  182. )
  183. read_count += chunk_size
  184. if not chunk_size:
  185. if not read_count:
  186. return SecurityConst.errSSLClosedGraceful
  187. break
  188. except (socket.error) as e:
  189. error = e.errno
  190. if error is not None and error != errno.EAGAIN:
  191. if error == errno.ECONNRESET:
  192. return SecurityConst.errSSLClosedAbort
  193. raise
  194. data_length_pointer[0] = read_count
  195. if read_count != requested_length:
  196. return SecurityConst.errSSLWouldBlock
  197. return 0
  198. except Exception as e:
  199. if wrapped_socket is not None:
  200. wrapped_socket._exception = e
  201. return SecurityConst.errSSLInternal
  202. def _write_callback(connection_id, data_buffer, data_length_pointer):
  203. """
  204. SecureTransport write callback. This is called by ST to request that data
  205. actually be sent on the network.
  206. """
  207. wrapped_socket = None
  208. try:
  209. wrapped_socket = _connection_refs.get(connection_id)
  210. if wrapped_socket is None:
  211. return SecurityConst.errSSLInternal
  212. base_socket = wrapped_socket.socket
  213. bytes_to_write = data_length_pointer[0]
  214. data = ctypes.string_at(data_buffer, bytes_to_write)
  215. timeout = wrapped_socket.gettimeout()
  216. error = None
  217. sent = 0
  218. try:
  219. while sent < bytes_to_write:
  220. if timeout is None or timeout >= 0:
  221. writables = util.wait_for_write([base_socket], timeout)
  222. if not writables:
  223. raise socket.error(errno.EAGAIN, 'timed out')
  224. chunk_sent = base_socket.send(data)
  225. sent += chunk_sent
  226. # This has some needless copying here, but I'm not sure there's
  227. # much value in optimising this data path.
  228. data = data[chunk_sent:]
  229. except (socket.error) as e:
  230. error = e.errno
  231. if error is not None and error != errno.EAGAIN:
  232. if error == errno.ECONNRESET:
  233. return SecurityConst.errSSLClosedAbort
  234. raise
  235. data_length_pointer[0] = sent
  236. if sent != bytes_to_write:
  237. return SecurityConst.errSSLWouldBlock
  238. return 0
  239. except Exception as e:
  240. if wrapped_socket is not None:
  241. wrapped_socket._exception = e
  242. return SecurityConst.errSSLInternal
  243. # We need to keep these two objects references alive: if they get GC'd while
  244. # in use then SecureTransport could attempt to call a function that is in freed
  245. # memory. That would be...uh...bad. Yeah, that's the word. Bad.
  246. _read_callback_pointer = Security.SSLReadFunc(_read_callback)
  247. _write_callback_pointer = Security.SSLWriteFunc(_write_callback)
  248. class WrappedSocket(object):
  249. """
  250. API-compatibility wrapper for Python's OpenSSL wrapped socket object.
  251. Note: _makefile_refs, _drop(), and _reuse() are needed for the garbage
  252. collector of PyPy.
  253. """
  254. def __init__(self, socket):
  255. self.socket = socket
  256. self.context = None
  257. self._makefile_refs = 0
  258. self._closed = False
  259. self._exception = None
  260. self._keychain = None
  261. self._keychain_dir = None
  262. self._client_cert_chain = None
  263. # We save off the previously-configured timeout and then set it to
  264. # zero. This is done because we use select and friends to handle the
  265. # timeouts, but if we leave the timeout set on the lower socket then
  266. # Python will "kindly" call select on that socket again for us. Avoid
  267. # that by forcing the timeout to zero.
  268. self._timeout = self.socket.gettimeout()
  269. self.socket.settimeout(0)
  270. @contextlib.contextmanager
  271. def _raise_on_error(self):
  272. """
  273. A context manager that can be used to wrap calls that do I/O from
  274. SecureTransport. If any of the I/O callbacks hit an exception, this
  275. context manager will correctly propagate the exception after the fact.
  276. This avoids silently swallowing those exceptions.
  277. It also correctly forces the socket closed.
  278. """
  279. self._exception = None
  280. # We explicitly don't catch around this yield because in the unlikely
  281. # event that an exception was hit in the block we don't want to swallow
  282. # it.
  283. yield
  284. if self._exception is not None:
  285. exception, self._exception = self._exception, None
  286. self.close()
  287. raise exception
  288. def _set_ciphers(self):
  289. """
  290. Sets up the allowed ciphers. By default this matches the set in
  291. util.ssl_.DEFAULT_CIPHERS, at least as supported by macOS. This is done
  292. custom and doesn't allow changing at this time, mostly because parsing
  293. OpenSSL cipher strings is going to be a freaking nightmare.
  294. """
  295. ciphers = (Security.SSLCipherSuite * len(CIPHER_SUITES))(*CIPHER_SUITES)
  296. result = Security.SSLSetEnabledCiphers(
  297. self.context, ciphers, len(CIPHER_SUITES)
  298. )
  299. _assert_no_error(result)
  300. def _custom_validate(self, verify, trust_bundle):
  301. """
  302. Called when we have set custom validation. We do this in two cases:
  303. first, when cert validation is entirely disabled; and second, when
  304. using a custom trust DB.
  305. """
  306. # If we disabled cert validation, just say: cool.
  307. if not verify:
  308. return
  309. # We want data in memory, so load it up.
  310. if os.path.isfile(trust_bundle):
  311. with open(trust_bundle, 'rb') as f:
  312. trust_bundle = f.read()
  313. cert_array = None
  314. trust = Security.SecTrustRef()
  315. try:
  316. # Get a CFArray that contains the certs we want.
  317. cert_array = _cert_array_from_pem(trust_bundle)
  318. # Ok, now the hard part. We want to get the SecTrustRef that ST has
  319. # created for this connection, shove our CAs into it, tell ST to
  320. # ignore everything else it knows, and then ask if it can build a
  321. # chain. This is a buuuunch of code.
  322. result = Security.SSLCopyPeerTrust(
  323. self.context, ctypes.byref(trust)
  324. )
  325. _assert_no_error(result)
  326. if not trust:
  327. raise ssl.SSLError("Failed to copy trust reference")
  328. result = Security.SecTrustSetAnchorCertificates(trust, cert_array)
  329. _assert_no_error(result)
  330. result = Security.SecTrustSetAnchorCertificatesOnly(trust, True)
  331. _assert_no_error(result)
  332. trust_result = Security.SecTrustResultType()
  333. result = Security.SecTrustEvaluate(
  334. trust, ctypes.byref(trust_result)
  335. )
  336. _assert_no_error(result)
  337. finally:
  338. if trust:
  339. CoreFoundation.CFRelease(trust)
  340. if cert_array is None:
  341. CoreFoundation.CFRelease(cert_array)
  342. # Ok, now we can look at what the result was.
  343. successes = (
  344. SecurityConst.kSecTrustResultUnspecified,
  345. SecurityConst.kSecTrustResultProceed
  346. )
  347. if trust_result.value not in successes:
  348. raise ssl.SSLError(
  349. "certificate verify failed, error code: %d" %
  350. trust_result.value
  351. )
  352. def handshake(self,
  353. server_hostname,
  354. verify,
  355. trust_bundle,
  356. min_version,
  357. max_version,
  358. client_cert,
  359. client_key,
  360. client_key_passphrase):
  361. """
  362. Actually performs the TLS handshake. This is run automatically by
  363. wrapped socket, and shouldn't be needed in user code.
  364. """
  365. # First, we do the initial bits of connection setup. We need to create
  366. # a context, set its I/O funcs, and set the connection reference.
  367. self.context = Security.SSLCreateContext(
  368. None, SecurityConst.kSSLClientSide, SecurityConst.kSSLStreamType
  369. )
  370. result = Security.SSLSetIOFuncs(
  371. self.context, _read_callback_pointer, _write_callback_pointer
  372. )
  373. _assert_no_error(result)
  374. # Here we need to compute the handle to use. We do this by taking the
  375. # id of self modulo 2**31 - 1. If this is already in the dictionary, we
  376. # just keep incrementing by one until we find a free space.
  377. with _connection_ref_lock:
  378. handle = id(self) % 2147483647
  379. while handle in _connection_refs:
  380. handle = (handle + 1) % 2147483647
  381. _connection_refs[handle] = self
  382. result = Security.SSLSetConnection(self.context, handle)
  383. _assert_no_error(result)
  384. # If we have a server hostname, we should set that too.
  385. if server_hostname:
  386. if not isinstance(server_hostname, bytes):
  387. server_hostname = server_hostname.encode('utf-8')
  388. result = Security.SSLSetPeerDomainName(
  389. self.context, server_hostname, len(server_hostname)
  390. )
  391. _assert_no_error(result)
  392. # Setup the ciphers.
  393. self._set_ciphers()
  394. # Set the minimum and maximum TLS versions.
  395. result = Security.SSLSetProtocolVersionMin(self.context, min_version)
  396. _assert_no_error(result)
  397. result = Security.SSLSetProtocolVersionMax(self.context, max_version)
  398. _assert_no_error(result)
  399. # If there's a trust DB, we need to use it. We do that by telling
  400. # SecureTransport to break on server auth. We also do that if we don't
  401. # want to validate the certs at all: we just won't actually do any
  402. # authing in that case.
  403. if not verify or trust_bundle is not None:
  404. result = Security.SSLSetSessionOption(
  405. self.context,
  406. SecurityConst.kSSLSessionOptionBreakOnServerAuth,
  407. True
  408. )
  409. _assert_no_error(result)
  410. # If there's a client cert, we need to use it.
  411. if client_cert:
  412. self._keychain, self._keychain_dir = _temporary_keychain()
  413. self._client_cert_chain = _load_client_cert_chain(
  414. self._keychain, client_cert, client_key
  415. )
  416. result = Security.SSLSetCertificate(
  417. self.context, self._client_cert_chain
  418. )
  419. _assert_no_error(result)
  420. while True:
  421. with self._raise_on_error():
  422. result = Security.SSLHandshake(self.context)
  423. if result == SecurityConst.errSSLWouldBlock:
  424. raise socket.timeout("handshake timed out")
  425. elif result == SecurityConst.errSSLServerAuthCompleted:
  426. self._custom_validate(verify, trust_bundle)
  427. continue
  428. else:
  429. _assert_no_error(result)
  430. break
  431. def fileno(self):
  432. return self.socket.fileno()
  433. # Copy-pasted from Python 3.5 source code
  434. def _decref_socketios(self):
  435. if self._makefile_refs > 0:
  436. self._makefile_refs -= 1
  437. if self._closed:
  438. self.close()
  439. def recv(self, bufsiz):
  440. buffer = ctypes.create_string_buffer(bufsiz)
  441. bytes_read = self.recv_into(buffer, bufsiz)
  442. data = buffer[:bytes_read]
  443. return data
  444. def recv_into(self, buffer, nbytes=None):
  445. # Read short on EOF.
  446. if self._closed:
  447. return 0
  448. if nbytes is None:
  449. nbytes = len(buffer)
  450. buffer = (ctypes.c_char * nbytes).from_buffer(buffer)
  451. processed_bytes = ctypes.c_size_t(0)
  452. with self._raise_on_error():
  453. result = Security.SSLRead(
  454. self.context, buffer, nbytes, ctypes.byref(processed_bytes)
  455. )
  456. # There are some result codes that we want to treat as "not always
  457. # errors". Specifically, those are errSSLWouldBlock,
  458. # errSSLClosedGraceful, and errSSLClosedNoNotify.
  459. if (result == SecurityConst.errSSLWouldBlock):
  460. # If we didn't process any bytes, then this was just a time out.
  461. # However, we can get errSSLWouldBlock in situations when we *did*
  462. # read some data, and in those cases we should just read "short"
  463. # and return.
  464. if processed_bytes.value == 0:
  465. # Timed out, no data read.
  466. raise socket.timeout("recv timed out")
  467. elif result in (SecurityConst.errSSLClosedGraceful, SecurityConst.errSSLClosedNoNotify):
  468. # The remote peer has closed this connection. We should do so as
  469. # well. Note that we don't actually return here because in
  470. # principle this could actually be fired along with return data.
  471. # It's unlikely though.
  472. self.close()
  473. else:
  474. _assert_no_error(result)
  475. # Ok, we read and probably succeeded. We should return whatever data
  476. # was actually read.
  477. return processed_bytes.value
  478. def settimeout(self, timeout):
  479. self._timeout = timeout
  480. def gettimeout(self):
  481. return self._timeout
  482. def send(self, data):
  483. processed_bytes = ctypes.c_size_t(0)
  484. with self._raise_on_error():
  485. result = Security.SSLWrite(
  486. self.context, data, len(data), ctypes.byref(processed_bytes)
  487. )
  488. if result == SecurityConst.errSSLWouldBlock and processed_bytes.value == 0:
  489. # Timed out
  490. raise socket.timeout("send timed out")
  491. else:
  492. _assert_no_error(result)
  493. # We sent, and probably succeeded. Tell them how much we sent.
  494. return processed_bytes.value
  495. def sendall(self, data):
  496. total_sent = 0
  497. while total_sent < len(data):
  498. sent = self.send(data[total_sent:total_sent + SSL_WRITE_BLOCKSIZE])
  499. total_sent += sent
  500. def shutdown(self):
  501. with self._raise_on_error():
  502. Security.SSLClose(self.context)
  503. def close(self):
  504. # TODO: should I do clean shutdown here? Do I have to?
  505. if self._makefile_refs < 1:
  506. self._closed = True
  507. if self.context:
  508. CoreFoundation.CFRelease(self.context)
  509. self.context = None
  510. if self._client_cert_chain:
  511. CoreFoundation.CFRelease(self._client_cert_chain)
  512. self._client_cert_chain = None
  513. if self._keychain:
  514. Security.SecKeychainDelete(self._keychain)
  515. CoreFoundation.CFRelease(self._keychain)
  516. shutil.rmtree(self._keychain_dir)
  517. self._keychain = self._keychain_dir = None
  518. return self.socket.close()
  519. else:
  520. self._makefile_refs -= 1
  521. def getpeercert(self, binary_form=False):
  522. # Urgh, annoying.
  523. #
  524. # Here's how we do this:
  525. #
  526. # 1. Call SSLCopyPeerTrust to get hold of the trust object for this
  527. # connection.
  528. # 2. Call SecTrustGetCertificateAtIndex for index 0 to get the leaf.
  529. # 3. To get the CN, call SecCertificateCopyCommonName and process that
  530. # string so that it's of the appropriate type.
  531. # 4. To get the SAN, we need to do something a bit more complex:
  532. # a. Call SecCertificateCopyValues to get the data, requesting
  533. # kSecOIDSubjectAltName.
  534. # b. Mess about with this dictionary to try to get the SANs out.
  535. #
  536. # This is gross. Really gross. It's going to be a few hundred LoC extra
  537. # just to repeat something that SecureTransport can *already do*. So my
  538. # operating assumption at this time is that what we want to do is
  539. # instead to just flag to urllib3 that it shouldn't do its own hostname
  540. # validation when using SecureTransport.
  541. if not binary_form:
  542. raise ValueError(
  543. "SecureTransport only supports dumping binary certs"
  544. )
  545. trust = Security.SecTrustRef()
  546. certdata = None
  547. der_bytes = None
  548. try:
  549. # Grab the trust store.
  550. result = Security.SSLCopyPeerTrust(
  551. self.context, ctypes.byref(trust)
  552. )
  553. _assert_no_error(result)
  554. if not trust:
  555. # Probably we haven't done the handshake yet. No biggie.
  556. return None
  557. cert_count = Security.SecTrustGetCertificateCount(trust)
  558. if not cert_count:
  559. # Also a case that might happen if we haven't handshaked.
  560. # Handshook? Handshaken?
  561. return None
  562. leaf = Security.SecTrustGetCertificateAtIndex(trust, 0)
  563. assert leaf
  564. # Ok, now we want the DER bytes.
  565. certdata = Security.SecCertificateCopyData(leaf)
  566. assert certdata
  567. data_length = CoreFoundation.CFDataGetLength(certdata)
  568. data_buffer = CoreFoundation.CFDataGetBytePtr(certdata)
  569. der_bytes = ctypes.string_at(data_buffer, data_length)
  570. finally:
  571. if certdata:
  572. CoreFoundation.CFRelease(certdata)
  573. if trust:
  574. CoreFoundation.CFRelease(trust)
  575. return der_bytes
  576. def _reuse(self):
  577. self._makefile_refs += 1
  578. def _drop(self):
  579. if self._makefile_refs < 1:
  580. self.close()
  581. else:
  582. self._makefile_refs -= 1
  583. if _fileobject: # Platform-specific: Python 2
  584. def makefile(self, mode, bufsize=-1):
  585. self._makefile_refs += 1
  586. return _fileobject(self, mode, bufsize, close=True)
  587. else: # Platform-specific: Python 3
  588. def makefile(self, mode="r", buffering=None, *args, **kwargs):
  589. # We disable buffering with SecureTransport because it conflicts with
  590. # the buffering that ST does internally (see issue #1153 for more).
  591. buffering = 0
  592. return backport_makefile(self, mode, buffering, *args, **kwargs)
  593. WrappedSocket.makefile = makefile
  594. class SecureTransportContext(object):
  595. """
  596. I am a wrapper class for the SecureTransport library, to translate the
  597. interface of the standard library ``SSLContext`` object to calls into
  598. SecureTransport.
  599. """
  600. def __init__(self, protocol):
  601. self._min_version, self._max_version = _protocol_to_min_max[protocol]
  602. self._options = 0
  603. self._verify = False
  604. self._trust_bundle = None
  605. self._client_cert = None
  606. self._client_key = None
  607. self._client_key_passphrase = None
  608. @property
  609. def check_hostname(self):
  610. """
  611. SecureTransport cannot have its hostname checking disabled. For more,
  612. see the comment on getpeercert() in this file.
  613. """
  614. return True
  615. @check_hostname.setter
  616. def check_hostname(self, value):
  617. """
  618. SecureTransport cannot have its hostname checking disabled. For more,
  619. see the comment on getpeercert() in this file.
  620. """
  621. pass
  622. @property
  623. def options(self):
  624. # TODO: Well, crap.
  625. #
  626. # So this is the bit of the code that is the most likely to cause us
  627. # trouble. Essentially we need to enumerate all of the SSL options that
  628. # users might want to use and try to see if we can sensibly translate
  629. # them, or whether we should just ignore them.
  630. return self._options
  631. @options.setter
  632. def options(self, value):
  633. # TODO: Update in line with above.
  634. self._options = value
  635. @property
  636. def verify_mode(self):
  637. return ssl.CERT_REQUIRED if self._verify else ssl.CERT_NONE
  638. @verify_mode.setter
  639. def verify_mode(self, value):
  640. self._verify = True if value == ssl.CERT_REQUIRED else False
  641. def set_default_verify_paths(self):
  642. # So, this has to do something a bit weird. Specifically, what it does
  643. # is nothing.
  644. #
  645. # This means that, if we had previously had load_verify_locations
  646. # called, this does not undo that. We need to do that because it turns
  647. # out that the rest of the urllib3 code will attempt to load the
  648. # default verify paths if it hasn't been told about any paths, even if
  649. # the context itself was sometime earlier. We resolve that by just
  650. # ignoring it.
  651. pass
  652. def load_default_certs(self):
  653. return self.set_default_verify_paths()
  654. def set_ciphers(self, ciphers):
  655. # For now, we just require the default cipher string.
  656. if ciphers != util.ssl_.DEFAULT_CIPHERS:
  657. raise ValueError(
  658. "SecureTransport doesn't support custom cipher strings"
  659. )
  660. def load_verify_locations(self, cafile=None, capath=None, cadata=None):
  661. # OK, we only really support cadata and cafile.
  662. if capath is not None:
  663. raise ValueError(
  664. "SecureTransport does not support cert directories"
  665. )
  666. self._trust_bundle = cafile or cadata
  667. def load_cert_chain(self, certfile, keyfile=None, password=None):
  668. self._client_cert = certfile
  669. self._client_key = keyfile
  670. self._client_cert_passphrase = password
  671. def wrap_socket(self, sock, server_side=False,
  672. do_handshake_on_connect=True, suppress_ragged_eofs=True,
  673. server_hostname=None):
  674. # So, what do we do here? Firstly, we assert some properties. This is a
  675. # stripped down shim, so there is some functionality we don't support.
  676. # See PEP 543 for the real deal.
  677. assert not server_side
  678. assert do_handshake_on_connect
  679. assert suppress_ragged_eofs
  680. # Ok, we're good to go. Now we want to create the wrapped socket object
  681. # and store it in the appropriate place.
  682. wrapped_socket = WrappedSocket(sock)
  683. # Now we can handshake
  684. wrapped_socket.handshake(
  685. server_hostname, self._verify, self._trust_bundle,
  686. self._min_version, self._max_version, self._client_cert,
  687. self._client_key, self._client_key_passphrase
  688. )
  689. return wrapped_socket