webull-openapi-python-sdk 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (295) hide show
  1. samples/__init__.py +1 -0
  2. samples/data/__init__.py +1 -0
  3. samples/data/data_client.py +57 -0
  4. samples/data/data_streaming_client.py +86 -0
  5. samples/data/data_streaming_client_async.py +101 -0
  6. samples/trade/__init__.py +0 -0
  7. samples/trade/trade_client.py +163 -0
  8. samples/trade/trade_client_v2.py +181 -0
  9. samples/trade/trade_event_client.py +47 -0
  10. webull/__init__.py +1 -0
  11. webull/core/__init__.py +12 -0
  12. webull/core/auth/__init__.py +0 -0
  13. webull/core/auth/algorithm/__init__.py +0 -0
  14. webull/core/auth/algorithm/sha_hmac1.py +65 -0
  15. webull/core/auth/algorithm/sha_hmac256.py +75 -0
  16. webull/core/auth/composer/__init__.py +0 -0
  17. webull/core/auth/composer/default_signature_composer.py +125 -0
  18. webull/core/auth/credentials.py +46 -0
  19. webull/core/auth/signers/__init__.py +0 -0
  20. webull/core/auth/signers/app_key_signer.py +72 -0
  21. webull/core/auth/signers/signer.py +48 -0
  22. webull/core/auth/signers/signer_factory.py +58 -0
  23. webull/core/cache/__init__.py +225 -0
  24. webull/core/client.py +410 -0
  25. webull/core/common/__init__.py +0 -0
  26. webull/core/common/api_type.py +19 -0
  27. webull/core/common/easy_enum.py +35 -0
  28. webull/core/common/region.py +7 -0
  29. webull/core/compat.py +85 -0
  30. webull/core/context/__init__.py +0 -0
  31. webull/core/context/request_context_holder.py +33 -0
  32. webull/core/data/endpoints.json +22 -0
  33. webull/core/data/retry_config.json +15 -0
  34. webull/core/endpoint/__init__.py +8 -0
  35. webull/core/endpoint/chained_endpoint_resolver.py +57 -0
  36. webull/core/endpoint/default_endpoint_resolver.py +60 -0
  37. webull/core/endpoint/local_config_regional_endpoint_resolver.py +77 -0
  38. webull/core/endpoint/resolver_endpoint_request.py +46 -0
  39. webull/core/endpoint/user_customized_endpoint_resolver.py +55 -0
  40. webull/core/exception/__init__.py +0 -0
  41. webull/core/exception/error_code.py +23 -0
  42. webull/core/exception/error_msg.py +21 -0
  43. webull/core/exception/exceptions.py +53 -0
  44. webull/core/headers.py +57 -0
  45. webull/core/http/__init__.py +0 -0
  46. webull/core/http/initializer/__init__.py +0 -0
  47. webull/core/http/initializer/client_initializer.py +79 -0
  48. webull/core/http/initializer/token/__init__.py +0 -0
  49. webull/core/http/initializer/token/bean/__init__.py +0 -0
  50. webull/core/http/initializer/token/bean/access_token.py +40 -0
  51. webull/core/http/initializer/token/bean/check_token_request.py +44 -0
  52. webull/core/http/initializer/token/bean/create_token_request.py +45 -0
  53. webull/core/http/initializer/token/bean/refresh_token_request.py +44 -0
  54. webull/core/http/initializer/token/token_manager.py +208 -0
  55. webull/core/http/initializer/token/token_operation.py +72 -0
  56. webull/core/http/method_type.py +43 -0
  57. webull/core/http/protocol_type.py +43 -0
  58. webull/core/http/request.py +121 -0
  59. webull/core/http/response.py +166 -0
  60. webull/core/request.py +278 -0
  61. webull/core/retry/__init__.py +0 -0
  62. webull/core/retry/backoff_strategy.py +102 -0
  63. webull/core/retry/retry_condition.py +214 -0
  64. webull/core/retry/retry_policy.py +63 -0
  65. webull/core/retry/retry_policy_context.py +51 -0
  66. webull/core/utils/__init__.py +0 -0
  67. webull/core/utils/common.py +62 -0
  68. webull/core/utils/data.py +25 -0
  69. webull/core/utils/desensitize.py +33 -0
  70. webull/core/utils/validation.py +49 -0
  71. webull/core/vendored/__init__.py +0 -0
  72. webull/core/vendored/requests/__init__.py +94 -0
  73. webull/core/vendored/requests/__version__.py +28 -0
  74. webull/core/vendored/requests/_internal_utils.py +56 -0
  75. webull/core/vendored/requests/adapters.py +539 -0
  76. webull/core/vendored/requests/api.py +166 -0
  77. webull/core/vendored/requests/auth.py +307 -0
  78. webull/core/vendored/requests/certs.py +34 -0
  79. webull/core/vendored/requests/compat.py +85 -0
  80. webull/core/vendored/requests/cookies.py +555 -0
  81. webull/core/vendored/requests/exceptions.py +136 -0
  82. webull/core/vendored/requests/help.py +134 -0
  83. webull/core/vendored/requests/hooks.py +48 -0
  84. webull/core/vendored/requests/models.py +960 -0
  85. webull/core/vendored/requests/packages/__init__.py +17 -0
  86. webull/core/vendored/requests/packages/certifi/__init__.py +17 -0
  87. webull/core/vendored/requests/packages/certifi/__main__.py +16 -0
  88. webull/core/vendored/requests/packages/certifi/cacert.pem +4433 -0
  89. webull/core/vendored/requests/packages/certifi/core.py +51 -0
  90. webull/core/vendored/requests/packages/chardet/__init__.py +53 -0
  91. webull/core/vendored/requests/packages/chardet/big5freq.py +400 -0
  92. webull/core/vendored/requests/packages/chardet/big5prober.py +61 -0
  93. webull/core/vendored/requests/packages/chardet/chardistribution.py +247 -0
  94. webull/core/vendored/requests/packages/chardet/charsetgroupprober.py +120 -0
  95. webull/core/vendored/requests/packages/chardet/charsetprober.py +159 -0
  96. webull/core/vendored/requests/packages/chardet/cli/__init__.py +1 -0
  97. webull/core/vendored/requests/packages/chardet/cli/chardetect.py +99 -0
  98. webull/core/vendored/requests/packages/chardet/codingstatemachine.py +102 -0
  99. webull/core/vendored/requests/packages/chardet/compat.py +48 -0
  100. webull/core/vendored/requests/packages/chardet/cp949prober.py +63 -0
  101. webull/core/vendored/requests/packages/chardet/enums.py +90 -0
  102. webull/core/vendored/requests/packages/chardet/escprober.py +115 -0
  103. webull/core/vendored/requests/packages/chardet/escsm.py +260 -0
  104. webull/core/vendored/requests/packages/chardet/eucjpprober.py +106 -0
  105. webull/core/vendored/requests/packages/chardet/euckrfreq.py +209 -0
  106. webull/core/vendored/requests/packages/chardet/euckrprober.py +61 -0
  107. webull/core/vendored/requests/packages/chardet/euctwfreq.py +401 -0
  108. webull/core/vendored/requests/packages/chardet/euctwprober.py +60 -0
  109. webull/core/vendored/requests/packages/chardet/gb2312freq.py +297 -0
  110. webull/core/vendored/requests/packages/chardet/gb2312prober.py +60 -0
  111. webull/core/vendored/requests/packages/chardet/hebrewprober.py +306 -0
  112. webull/core/vendored/requests/packages/chardet/jisfreq.py +339 -0
  113. webull/core/vendored/requests/packages/chardet/jpcntx.py +247 -0
  114. webull/core/vendored/requests/packages/chardet/langbulgarianmodel.py +242 -0
  115. webull/core/vendored/requests/packages/chardet/langcyrillicmodel.py +347 -0
  116. webull/core/vendored/requests/packages/chardet/langgreekmodel.py +239 -0
  117. webull/core/vendored/requests/packages/chardet/langhebrewmodel.py +214 -0
  118. webull/core/vendored/requests/packages/chardet/langhungarianmodel.py +239 -0
  119. webull/core/vendored/requests/packages/chardet/langthaimodel.py +213 -0
  120. webull/core/vendored/requests/packages/chardet/langturkishmodel.py +207 -0
  121. webull/core/vendored/requests/packages/chardet/latin1prober.py +159 -0
  122. webull/core/vendored/requests/packages/chardet/mbcharsetprober.py +105 -0
  123. webull/core/vendored/requests/packages/chardet/mbcsgroupprober.py +68 -0
  124. webull/core/vendored/requests/packages/chardet/mbcssm.py +586 -0
  125. webull/core/vendored/requests/packages/chardet/sbcharsetprober.py +146 -0
  126. webull/core/vendored/requests/packages/chardet/sbcsgroupprober.py +87 -0
  127. webull/core/vendored/requests/packages/chardet/sjisprober.py +106 -0
  128. webull/core/vendored/requests/packages/chardet/universaldetector.py +300 -0
  129. webull/core/vendored/requests/packages/chardet/utf8prober.py +96 -0
  130. webull/core/vendored/requests/packages/chardet/version.py +23 -0
  131. webull/core/vendored/requests/packages/urllib3/__init__.py +114 -0
  132. webull/core/vendored/requests/packages/urllib3/_collections.py +346 -0
  133. webull/core/vendored/requests/packages/urllib3/connection.py +405 -0
  134. webull/core/vendored/requests/packages/urllib3/connectionpool.py +910 -0
  135. webull/core/vendored/requests/packages/urllib3/contrib/__init__.py +0 -0
  136. webull/core/vendored/requests/packages/urllib3/contrib/_appengine_environ.py +44 -0
  137. webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/__init__.py +0 -0
  138. webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/bindings.py +607 -0
  139. webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/low_level.py +360 -0
  140. webull/core/vendored/requests/packages/urllib3/contrib/appengine.py +303 -0
  141. webull/core/vendored/requests/packages/urllib3/contrib/ntlmpool.py +125 -0
  142. webull/core/vendored/requests/packages/urllib3/contrib/pyopenssl.py +484 -0
  143. webull/core/vendored/requests/packages/urllib3/contrib/securetransport.py +818 -0
  144. webull/core/vendored/requests/packages/urllib3/contrib/socks.py +206 -0
  145. webull/core/vendored/requests/packages/urllib3/exceptions.py +260 -0
  146. webull/core/vendored/requests/packages/urllib3/fields.py +192 -0
  147. webull/core/vendored/requests/packages/urllib3/filepost.py +112 -0
  148. webull/core/vendored/requests/packages/urllib3/packages/__init__.py +19 -0
  149. webull/core/vendored/requests/packages/urllib3/packages/backports/__init__.py +0 -0
  150. webull/core/vendored/requests/packages/urllib3/packages/backports/makefile.py +67 -0
  151. webull/core/vendored/requests/packages/urllib3/packages/ordered_dict.py +273 -0
  152. webull/core/vendored/requests/packages/urllib3/packages/six.py +882 -0
  153. webull/core/vendored/requests/packages/urllib3/packages/socks.py +887 -0
  154. webull/core/vendored/requests/packages/urllib3/packages/ssl_match_hostname/__init__.py +19 -0
  155. webull/core/vendored/requests/packages/urllib3/packages/ssl_match_hostname/_implementation.py +170 -0
  156. webull/core/vendored/requests/packages/urllib3/poolmanager.py +467 -0
  157. webull/core/vendored/requests/packages/urllib3/request.py +164 -0
  158. webull/core/vendored/requests/packages/urllib3/response.py +721 -0
  159. webull/core/vendored/requests/packages/urllib3/util/__init__.py +68 -0
  160. webull/core/vendored/requests/packages/urllib3/util/connection.py +148 -0
  161. webull/core/vendored/requests/packages/urllib3/util/queue.py +35 -0
  162. webull/core/vendored/requests/packages/urllib3/util/request.py +132 -0
  163. webull/core/vendored/requests/packages/urllib3/util/response.py +101 -0
  164. webull/core/vendored/requests/packages/urllib3/util/retry.py +426 -0
  165. webull/core/vendored/requests/packages/urllib3/util/selectors.py +601 -0
  166. webull/core/vendored/requests/packages/urllib3/util/ssl_.py +396 -0
  167. webull/core/vendored/requests/packages/urllib3/util/timeout.py +256 -0
  168. webull/core/vendored/requests/packages/urllib3/util/url.py +252 -0
  169. webull/core/vendored/requests/packages/urllib3/util/wait.py +164 -0
  170. webull/core/vendored/requests/packages.py +28 -0
  171. webull/core/vendored/requests/sessions.py +750 -0
  172. webull/core/vendored/requests/status_codes.py +105 -0
  173. webull/core/vendored/requests/structures.py +119 -0
  174. webull/core/vendored/requests/utils.py +916 -0
  175. webull/core/vendored/six.py +905 -0
  176. webull/data/__init__.py +3 -0
  177. webull/data/common/__init__.py +0 -0
  178. webull/data/common/category.py +26 -0
  179. webull/data/common/connect_ack.py +29 -0
  180. webull/data/common/direction.py +25 -0
  181. webull/data/common/exchange_code.py +33 -0
  182. webull/data/common/exercise_style.py +22 -0
  183. webull/data/common/expiration_cycle.py +26 -0
  184. webull/data/common/instrument_status.py +23 -0
  185. webull/data/common/option_type.py +20 -0
  186. webull/data/common/subscribe_type.py +22 -0
  187. webull/data/common/timespan.py +29 -0
  188. webull/data/data_client.py +35 -0
  189. webull/data/data_streaming_client.py +89 -0
  190. webull/data/internal/__init__.py +0 -0
  191. webull/data/internal/default_retry_policy.py +84 -0
  192. webull/data/internal/exceptions.py +60 -0
  193. webull/data/internal/quotes_client.py +314 -0
  194. webull/data/internal/quotes_decoder.py +40 -0
  195. webull/data/internal/quotes_payload_decoder.py +35 -0
  196. webull/data/internal/quotes_topic.py +36 -0
  197. webull/data/quotes/__init__.py +0 -0
  198. webull/data/quotes/instrument.py +33 -0
  199. webull/data/quotes/market_data.py +187 -0
  200. webull/data/quotes/market_streaming_data.py +66 -0
  201. webull/data/quotes/subscribe/__init__.py +0 -0
  202. webull/data/quotes/subscribe/ask_bid_result.py +49 -0
  203. webull/data/quotes/subscribe/basic_result.py +45 -0
  204. webull/data/quotes/subscribe/broker_result.py +33 -0
  205. webull/data/quotes/subscribe/message_pb2.py +37 -0
  206. webull/data/quotes/subscribe/order_result.py +30 -0
  207. webull/data/quotes/subscribe/payload_type.py +19 -0
  208. webull/data/quotes/subscribe/quote_decoder.py +28 -0
  209. webull/data/quotes/subscribe/quote_result.py +47 -0
  210. webull/data/quotes/subscribe/snapshot_decoder.py +30 -0
  211. webull/data/quotes/subscribe/snapshot_result.py +69 -0
  212. webull/data/quotes/subscribe/tick_decoder.py +29 -0
  213. webull/data/quotes/subscribe/tick_result.py +47 -0
  214. webull/data/request/__init__.py +0 -0
  215. webull/data/request/get_batch_historical_bars_request.py +43 -0
  216. webull/data/request/get_corp_action_request.py +47 -0
  217. webull/data/request/get_eod_bars_request.py +32 -0
  218. webull/data/request/get_historical_bars_request.py +43 -0
  219. webull/data/request/get_instruments_request.py +30 -0
  220. webull/data/request/get_quotes_request.py +35 -0
  221. webull/data/request/get_snapshot_request.py +38 -0
  222. webull/data/request/get_tick_request.py +37 -0
  223. webull/data/request/subscribe_request.py +43 -0
  224. webull/data/request/unsubscribe_request.py +42 -0
  225. webull/trade/__init__.py +2 -0
  226. webull/trade/common/__init__.py +0 -0
  227. webull/trade/common/account_type.py +22 -0
  228. webull/trade/common/category.py +29 -0
  229. webull/trade/common/combo_ticker_type.py +23 -0
  230. webull/trade/common/combo_type.py +31 -0
  231. webull/trade/common/currency.py +24 -0
  232. webull/trade/common/forbid_reason.py +27 -0
  233. webull/trade/common/instrument_type.py +27 -0
  234. webull/trade/common/markets.py +27 -0
  235. webull/trade/common/order_entrust_type.py +21 -0
  236. webull/trade/common/order_side.py +23 -0
  237. webull/trade/common/order_status.py +25 -0
  238. webull/trade/common/order_tif.py +24 -0
  239. webull/trade/common/order_type.py +30 -0
  240. webull/trade/common/trade_policy.py +22 -0
  241. webull/trade/common/trading_date_type.py +24 -0
  242. webull/trade/common/trailing_type.py +23 -0
  243. webull/trade/events/__init__.py +0 -0
  244. webull/trade/events/default_retry_policy.py +64 -0
  245. webull/trade/events/events_pb2.py +43 -0
  246. webull/trade/events/events_pb2_grpc.py +66 -0
  247. webull/trade/events/signature_composer.py +61 -0
  248. webull/trade/events/types.py +21 -0
  249. webull/trade/request/__init__.py +0 -0
  250. webull/trade/request/cancel_order_request.py +28 -0
  251. webull/trade/request/get_account_balance_request.py +28 -0
  252. webull/trade/request/get_account_positions_request.py +30 -0
  253. webull/trade/request/get_account_profile_request.py +26 -0
  254. webull/trade/request/get_app_subscriptions.py +28 -0
  255. webull/trade/request/get_open_orders_request.py +30 -0
  256. webull/trade/request/get_order_detail_request.py +27 -0
  257. webull/trade/request/get_today_orders_request.py +31 -0
  258. webull/trade/request/get_trade_calendar_request.py +30 -0
  259. webull/trade/request/get_trade_instrument_detail_request.py +24 -0
  260. webull/trade/request/get_trade_security_detail_request.py +42 -0
  261. webull/trade/request/get_tradeable_instruments_request.py +27 -0
  262. webull/trade/request/palce_order_request.py +91 -0
  263. webull/trade/request/place_order_request_v2.py +58 -0
  264. webull/trade/request/replace_order_request.py +73 -0
  265. webull/trade/request/replace_order_request_v2.py +38 -0
  266. webull/trade/request/v2/__init__.py +0 -0
  267. webull/trade/request/v2/cancel_option_request.py +28 -0
  268. webull/trade/request/v2/cancel_order_request.py +28 -0
  269. webull/trade/request/v2/get_account_balance_request.py +28 -0
  270. webull/trade/request/v2/get_account_list.py +23 -0
  271. webull/trade/request/v2/get_account_positions_request.py +24 -0
  272. webull/trade/request/v2/get_order_detail_request.py +26 -0
  273. webull/trade/request/v2/get_order_history_request.py +35 -0
  274. webull/trade/request/v2/palce_order_request.py +87 -0
  275. webull/trade/request/v2/place_option_request.py +64 -0
  276. webull/trade/request/v2/preview_option_request.py +28 -0
  277. webull/trade/request/v2/preview_order_request.py +59 -0
  278. webull/trade/request/v2/replace_option_request.py +28 -0
  279. webull/trade/request/v2/replace_order_request.py +57 -0
  280. webull/trade/trade/__init__.py +0 -0
  281. webull/trade/trade/account_info.py +83 -0
  282. webull/trade/trade/order_operation.py +246 -0
  283. webull/trade/trade/trade_calendar.py +37 -0
  284. webull/trade/trade/trade_instrument.py +72 -0
  285. webull/trade/trade/v2/__init__.py +0 -0
  286. webull/trade/trade/v2/account_info_v2.py +55 -0
  287. webull/trade/trade/v2/order_operation_v2.py +206 -0
  288. webull/trade/trade_client.py +43 -0
  289. webull/trade/trade_events_client.py +233 -0
  290. webull_openapi_python_sdk-1.0.0.dist-info/METADATA +28 -0
  291. webull_openapi_python_sdk-1.0.0.dist-info/RECORD +295 -0
  292. webull_openapi_python_sdk-1.0.0.dist-info/WHEEL +5 -0
  293. webull_openapi_python_sdk-1.0.0.dist-info/licenses/LICENSE +202 -0
  294. webull_openapi_python_sdk-1.0.0.dist-info/licenses/NOTICE +56 -0
  295. webull_openapi_python_sdk-1.0.0.dist-info/top_level.txt +2 -0
@@ -0,0 +1,44 @@
1
+ # Copyright 2022 Webull
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ This module provides means to detect the App Engine environment.
17
+ """
18
+
19
+ import os
20
+
21
+
22
+ def is_appengine():
23
+ return (is_local_appengine() or
24
+ is_prod_appengine() or
25
+ is_prod_appengine_mvms())
26
+
27
+
28
+ def is_appengine_sandbox():
29
+ return is_appengine() and not is_prod_appengine_mvms()
30
+
31
+
32
+ def is_local_appengine():
33
+ return ('APPENGINE_RUNTIME' in os.environ and
34
+ 'Development/' in os.environ['SERVER_SOFTWARE'])
35
+
36
+
37
+ def is_prod_appengine():
38
+ return ('APPENGINE_RUNTIME' in os.environ and
39
+ 'Google App Engine/' in os.environ['SERVER_SOFTWARE'] and
40
+ not is_prod_appengine_mvms())
41
+
42
+
43
+ def is_prod_appengine_mvms():
44
+ return os.environ.get('GAE_VM', False) == 'true'
@@ -0,0 +1,607 @@
1
+ # Copyright 2022 Webull
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ This module uses ctypes to bind a whole bunch of functions and constants from
17
+ SecureTransport. The goal here is to provide the low-level API to
18
+ SecureTransport. These are essentially the C-level functions and constants, and
19
+ they're pretty gross to work with.
20
+
21
+ This code is a bastardised version of the code found in Will Bond's oscrypto
22
+ library. An enormous debt is owed to him for blazing this trail for us. For
23
+ that reason, this code should be considered to be covered both by urllib3's
24
+ license and by oscrypto's:
25
+
26
+ Copyright (c) 2015-2016 Will Bond <will@wbond.net>
27
+
28
+ Permission is hereby granted, free of charge, to any person obtaining a
29
+ copy of this software and associated documentation files (the "Software"),
30
+ to deal in the Software without restriction, including without limitation
31
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
32
+ and/or sell copies of the Software, and to permit persons to whom the
33
+ Software is furnished to do so, subject to the following conditions:
34
+
35
+ The above copyright notice and this permission notice shall be included in
36
+ all copies or substantial portions of the Software.
37
+
38
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
43
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
44
+ DEALINGS IN THE SOFTWARE.
45
+ """
46
+ from __future__ import absolute_import
47
+
48
+ import platform
49
+ from ctypes.util import find_library
50
+ from ctypes import (
51
+ c_void_p, c_int32, c_char_p, c_size_t, c_byte, c_uint32, c_ulong, c_long,
52
+ c_bool
53
+ )
54
+ from ctypes import CDLL, POINTER, CFUNCTYPE
55
+
56
+
57
+ security_path = find_library('Security')
58
+ if not security_path:
59
+ raise ImportError('The library Security could not be found')
60
+
61
+
62
+ core_foundation_path = find_library('CoreFoundation')
63
+ if not core_foundation_path:
64
+ raise ImportError('The library CoreFoundation could not be found')
65
+
66
+
67
+ version = platform.mac_ver()[0]
68
+ version_info = tuple(map(int, version.split('.')))
69
+ if version_info < (10, 8):
70
+ raise OSError(
71
+ 'Only OS X 10.8 and newer are supported, not %s.%s' % (
72
+ version_info[0], version_info[1]
73
+ )
74
+ )
75
+
76
+ Security = CDLL(security_path, use_errno=True)
77
+ CoreFoundation = CDLL(core_foundation_path, use_errno=True)
78
+
79
+ Boolean = c_bool
80
+ CFIndex = c_long
81
+ CFStringEncoding = c_uint32
82
+ CFData = c_void_p
83
+ CFString = c_void_p
84
+ CFArray = c_void_p
85
+ CFMutableArray = c_void_p
86
+ CFDictionary = c_void_p
87
+ CFError = c_void_p
88
+ CFType = c_void_p
89
+ CFTypeID = c_ulong
90
+
91
+ CFTypeRef = POINTER(CFType)
92
+ CFAllocatorRef = c_void_p
93
+
94
+ OSStatus = c_int32
95
+
96
+ CFDataRef = POINTER(CFData)
97
+ CFStringRef = POINTER(CFString)
98
+ CFArrayRef = POINTER(CFArray)
99
+ CFMutableArrayRef = POINTER(CFMutableArray)
100
+ CFDictionaryRef = POINTER(CFDictionary)
101
+ CFArrayCallBacks = c_void_p
102
+ CFDictionaryKeyCallBacks = c_void_p
103
+ CFDictionaryValueCallBacks = c_void_p
104
+
105
+ SecCertificateRef = POINTER(c_void_p)
106
+ SecExternalFormat = c_uint32
107
+ SecExternalItemType = c_uint32
108
+ SecIdentityRef = POINTER(c_void_p)
109
+ SecItemImportExportFlags = c_uint32
110
+ SecItemImportExportKeyParameters = c_void_p
111
+ SecKeychainRef = POINTER(c_void_p)
112
+ SSLProtocol = c_uint32
113
+ SSLCipherSuite = c_uint32
114
+ SSLContextRef = POINTER(c_void_p)
115
+ SecTrustRef = POINTER(c_void_p)
116
+ SSLConnectionRef = c_uint32
117
+ SecTrustResultType = c_uint32
118
+ SecTrustOptionFlags = c_uint32
119
+ SSLProtocolSide = c_uint32
120
+ SSLConnectionType = c_uint32
121
+ SSLSessionOption = c_uint32
122
+
123
+
124
+ try:
125
+ Security.SecItemImport.argtypes = [
126
+ CFDataRef,
127
+ CFStringRef,
128
+ POINTER(SecExternalFormat),
129
+ POINTER(SecExternalItemType),
130
+ SecItemImportExportFlags,
131
+ POINTER(SecItemImportExportKeyParameters),
132
+ SecKeychainRef,
133
+ POINTER(CFArrayRef),
134
+ ]
135
+ Security.SecItemImport.restype = OSStatus
136
+
137
+ Security.SecCertificateGetTypeID.argtypes = []
138
+ Security.SecCertificateGetTypeID.restype = CFTypeID
139
+
140
+ Security.SecIdentityGetTypeID.argtypes = []
141
+ Security.SecIdentityGetTypeID.restype = CFTypeID
142
+
143
+ Security.SecKeyGetTypeID.argtypes = []
144
+ Security.SecKeyGetTypeID.restype = CFTypeID
145
+
146
+ Security.SecCertificateCreateWithData.argtypes = [
147
+ CFAllocatorRef,
148
+ CFDataRef
149
+ ]
150
+ Security.SecCertificateCreateWithData.restype = SecCertificateRef
151
+
152
+ Security.SecCertificateCopyData.argtypes = [
153
+ SecCertificateRef
154
+ ]
155
+ Security.SecCertificateCopyData.restype = CFDataRef
156
+
157
+ Security.SecCopyErrorMessageString.argtypes = [
158
+ OSStatus,
159
+ c_void_p
160
+ ]
161
+ Security.SecCopyErrorMessageString.restype = CFStringRef
162
+
163
+ Security.SecIdentityCreateWithCertificate.argtypes = [
164
+ CFTypeRef,
165
+ SecCertificateRef,
166
+ POINTER(SecIdentityRef)
167
+ ]
168
+ Security.SecIdentityCreateWithCertificate.restype = OSStatus
169
+
170
+ Security.SecKeychainCreate.argtypes = [
171
+ c_char_p,
172
+ c_uint32,
173
+ c_void_p,
174
+ Boolean,
175
+ c_void_p,
176
+ POINTER(SecKeychainRef)
177
+ ]
178
+ Security.SecKeychainCreate.restype = OSStatus
179
+
180
+ Security.SecKeychainDelete.argtypes = [
181
+ SecKeychainRef
182
+ ]
183
+ Security.SecKeychainDelete.restype = OSStatus
184
+
185
+ Security.SecPKCS12Import.argtypes = [
186
+ CFDataRef,
187
+ CFDictionaryRef,
188
+ POINTER(CFArrayRef)
189
+ ]
190
+ Security.SecPKCS12Import.restype = OSStatus
191
+
192
+ SSLReadFunc = CFUNCTYPE(OSStatus, SSLConnectionRef, c_void_p, POINTER(c_size_t))
193
+ SSLWriteFunc = CFUNCTYPE(OSStatus, SSLConnectionRef, POINTER(c_byte), POINTER(c_size_t))
194
+
195
+ Security.SSLSetIOFuncs.argtypes = [
196
+ SSLContextRef,
197
+ SSLReadFunc,
198
+ SSLWriteFunc
199
+ ]
200
+ Security.SSLSetIOFuncs.restype = OSStatus
201
+
202
+ Security.SSLSetPeerID.argtypes = [
203
+ SSLContextRef,
204
+ c_char_p,
205
+ c_size_t
206
+ ]
207
+ Security.SSLSetPeerID.restype = OSStatus
208
+
209
+ Security.SSLSetCertificate.argtypes = [
210
+ SSLContextRef,
211
+ CFArrayRef
212
+ ]
213
+ Security.SSLSetCertificate.restype = OSStatus
214
+
215
+ Security.SSLSetCertificateAuthorities.argtypes = [
216
+ SSLContextRef,
217
+ CFTypeRef,
218
+ Boolean
219
+ ]
220
+ Security.SSLSetCertificateAuthorities.restype = OSStatus
221
+
222
+ Security.SSLSetConnection.argtypes = [
223
+ SSLContextRef,
224
+ SSLConnectionRef
225
+ ]
226
+ Security.SSLSetConnection.restype = OSStatus
227
+
228
+ Security.SSLSetPeerDomainName.argtypes = [
229
+ SSLContextRef,
230
+ c_char_p,
231
+ c_size_t
232
+ ]
233
+ Security.SSLSetPeerDomainName.restype = OSStatus
234
+
235
+ Security.SSLHandshake.argtypes = [
236
+ SSLContextRef
237
+ ]
238
+ Security.SSLHandshake.restype = OSStatus
239
+
240
+ Security.SSLRead.argtypes = [
241
+ SSLContextRef,
242
+ c_char_p,
243
+ c_size_t,
244
+ POINTER(c_size_t)
245
+ ]
246
+ Security.SSLRead.restype = OSStatus
247
+
248
+ Security.SSLWrite.argtypes = [
249
+ SSLContextRef,
250
+ c_char_p,
251
+ c_size_t,
252
+ POINTER(c_size_t)
253
+ ]
254
+ Security.SSLWrite.restype = OSStatus
255
+
256
+ Security.SSLClose.argtypes = [
257
+ SSLContextRef
258
+ ]
259
+ Security.SSLClose.restype = OSStatus
260
+
261
+ Security.SSLGetNumberSupportedCiphers.argtypes = [
262
+ SSLContextRef,
263
+ POINTER(c_size_t)
264
+ ]
265
+ Security.SSLGetNumberSupportedCiphers.restype = OSStatus
266
+
267
+ Security.SSLGetSupportedCiphers.argtypes = [
268
+ SSLContextRef,
269
+ POINTER(SSLCipherSuite),
270
+ POINTER(c_size_t)
271
+ ]
272
+ Security.SSLGetSupportedCiphers.restype = OSStatus
273
+
274
+ Security.SSLSetEnabledCiphers.argtypes = [
275
+ SSLContextRef,
276
+ POINTER(SSLCipherSuite),
277
+ c_size_t
278
+ ]
279
+ Security.SSLSetEnabledCiphers.restype = OSStatus
280
+
281
+ Security.SSLGetNumberEnabledCiphers.argtype = [
282
+ SSLContextRef,
283
+ POINTER(c_size_t)
284
+ ]
285
+ Security.SSLGetNumberEnabledCiphers.restype = OSStatus
286
+
287
+ Security.SSLGetEnabledCiphers.argtypes = [
288
+ SSLContextRef,
289
+ POINTER(SSLCipherSuite),
290
+ POINTER(c_size_t)
291
+ ]
292
+ Security.SSLGetEnabledCiphers.restype = OSStatus
293
+
294
+ Security.SSLGetNegotiatedCipher.argtypes = [
295
+ SSLContextRef,
296
+ POINTER(SSLCipherSuite)
297
+ ]
298
+ Security.SSLGetNegotiatedCipher.restype = OSStatus
299
+
300
+ Security.SSLGetNegotiatedProtocolVersion.argtypes = [
301
+ SSLContextRef,
302
+ POINTER(SSLProtocol)
303
+ ]
304
+ Security.SSLGetNegotiatedProtocolVersion.restype = OSStatus
305
+
306
+ Security.SSLCopyPeerTrust.argtypes = [
307
+ SSLContextRef,
308
+ POINTER(SecTrustRef)
309
+ ]
310
+ Security.SSLCopyPeerTrust.restype = OSStatus
311
+
312
+ Security.SecTrustSetAnchorCertificates.argtypes = [
313
+ SecTrustRef,
314
+ CFArrayRef
315
+ ]
316
+ Security.SecTrustSetAnchorCertificates.restype = OSStatus
317
+
318
+ Security.SecTrustSetAnchorCertificatesOnly.argstypes = [
319
+ SecTrustRef,
320
+ Boolean
321
+ ]
322
+ Security.SecTrustSetAnchorCertificatesOnly.restype = OSStatus
323
+
324
+ Security.SecTrustEvaluate.argtypes = [
325
+ SecTrustRef,
326
+ POINTER(SecTrustResultType)
327
+ ]
328
+ Security.SecTrustEvaluate.restype = OSStatus
329
+
330
+ Security.SecTrustGetCertificateCount.argtypes = [
331
+ SecTrustRef
332
+ ]
333
+ Security.SecTrustGetCertificateCount.restype = CFIndex
334
+
335
+ Security.SecTrustGetCertificateAtIndex.argtypes = [
336
+ SecTrustRef,
337
+ CFIndex
338
+ ]
339
+ Security.SecTrustGetCertificateAtIndex.restype = SecCertificateRef
340
+
341
+ Security.SSLCreateContext.argtypes = [
342
+ CFAllocatorRef,
343
+ SSLProtocolSide,
344
+ SSLConnectionType
345
+ ]
346
+ Security.SSLCreateContext.restype = SSLContextRef
347
+
348
+ Security.SSLSetSessionOption.argtypes = [
349
+ SSLContextRef,
350
+ SSLSessionOption,
351
+ Boolean
352
+ ]
353
+ Security.SSLSetSessionOption.restype = OSStatus
354
+
355
+ Security.SSLSetProtocolVersionMin.argtypes = [
356
+ SSLContextRef,
357
+ SSLProtocol
358
+ ]
359
+ Security.SSLSetProtocolVersionMin.restype = OSStatus
360
+
361
+ Security.SSLSetProtocolVersionMax.argtypes = [
362
+ SSLContextRef,
363
+ SSLProtocol
364
+ ]
365
+ Security.SSLSetProtocolVersionMax.restype = OSStatus
366
+
367
+ Security.SecCopyErrorMessageString.argtypes = [
368
+ OSStatus,
369
+ c_void_p
370
+ ]
371
+ Security.SecCopyErrorMessageString.restype = CFStringRef
372
+
373
+ Security.SSLReadFunc = SSLReadFunc
374
+ Security.SSLWriteFunc = SSLWriteFunc
375
+ Security.SSLContextRef = SSLContextRef
376
+ Security.SSLProtocol = SSLProtocol
377
+ Security.SSLCipherSuite = SSLCipherSuite
378
+ Security.SecIdentityRef = SecIdentityRef
379
+ Security.SecKeychainRef = SecKeychainRef
380
+ Security.SecTrustRef = SecTrustRef
381
+ Security.SecTrustResultType = SecTrustResultType
382
+ Security.SecExternalFormat = SecExternalFormat
383
+ Security.OSStatus = OSStatus
384
+
385
+ Security.kSecImportExportPassphrase = CFStringRef.in_dll(
386
+ Security, 'kSecImportExportPassphrase'
387
+ )
388
+ Security.kSecImportItemIdentity = CFStringRef.in_dll(
389
+ Security, 'kSecImportItemIdentity'
390
+ )
391
+
392
+ # CoreFoundation time!
393
+ CoreFoundation.CFRetain.argtypes = [
394
+ CFTypeRef
395
+ ]
396
+ CoreFoundation.CFRetain.restype = CFTypeRef
397
+
398
+ CoreFoundation.CFRelease.argtypes = [
399
+ CFTypeRef
400
+ ]
401
+ CoreFoundation.CFRelease.restype = None
402
+
403
+ CoreFoundation.CFGetTypeID.argtypes = [
404
+ CFTypeRef
405
+ ]
406
+ CoreFoundation.CFGetTypeID.restype = CFTypeID
407
+
408
+ CoreFoundation.CFStringCreateWithCString.argtypes = [
409
+ CFAllocatorRef,
410
+ c_char_p,
411
+ CFStringEncoding
412
+ ]
413
+ CoreFoundation.CFStringCreateWithCString.restype = CFStringRef
414
+
415
+ CoreFoundation.CFStringGetCStringPtr.argtypes = [
416
+ CFStringRef,
417
+ CFStringEncoding
418
+ ]
419
+ CoreFoundation.CFStringGetCStringPtr.restype = c_char_p
420
+
421
+ CoreFoundation.CFStringGetCString.argtypes = [
422
+ CFStringRef,
423
+ c_char_p,
424
+ CFIndex,
425
+ CFStringEncoding
426
+ ]
427
+ CoreFoundation.CFStringGetCString.restype = c_bool
428
+
429
+ CoreFoundation.CFDataCreate.argtypes = [
430
+ CFAllocatorRef,
431
+ c_char_p,
432
+ CFIndex
433
+ ]
434
+ CoreFoundation.CFDataCreate.restype = CFDataRef
435
+
436
+ CoreFoundation.CFDataGetLength.argtypes = [
437
+ CFDataRef
438
+ ]
439
+ CoreFoundation.CFDataGetLength.restype = CFIndex
440
+
441
+ CoreFoundation.CFDataGetBytePtr.argtypes = [
442
+ CFDataRef
443
+ ]
444
+ CoreFoundation.CFDataGetBytePtr.restype = c_void_p
445
+
446
+ CoreFoundation.CFDictionaryCreate.argtypes = [
447
+ CFAllocatorRef,
448
+ POINTER(CFTypeRef),
449
+ POINTER(CFTypeRef),
450
+ CFIndex,
451
+ CFDictionaryKeyCallBacks,
452
+ CFDictionaryValueCallBacks
453
+ ]
454
+ CoreFoundation.CFDictionaryCreate.restype = CFDictionaryRef
455
+
456
+ CoreFoundation.CFDictionaryGetValue.argtypes = [
457
+ CFDictionaryRef,
458
+ CFTypeRef
459
+ ]
460
+ CoreFoundation.CFDictionaryGetValue.restype = CFTypeRef
461
+
462
+ CoreFoundation.CFArrayCreate.argtypes = [
463
+ CFAllocatorRef,
464
+ POINTER(CFTypeRef),
465
+ CFIndex,
466
+ CFArrayCallBacks,
467
+ ]
468
+ CoreFoundation.CFArrayCreate.restype = CFArrayRef
469
+
470
+ CoreFoundation.CFArrayCreateMutable.argtypes = [
471
+ CFAllocatorRef,
472
+ CFIndex,
473
+ CFArrayCallBacks
474
+ ]
475
+ CoreFoundation.CFArrayCreateMutable.restype = CFMutableArrayRef
476
+
477
+ CoreFoundation.CFArrayAppendValue.argtypes = [
478
+ CFMutableArrayRef,
479
+ c_void_p
480
+ ]
481
+ CoreFoundation.CFArrayAppendValue.restype = None
482
+
483
+ CoreFoundation.CFArrayGetCount.argtypes = [
484
+ CFArrayRef
485
+ ]
486
+ CoreFoundation.CFArrayGetCount.restype = CFIndex
487
+
488
+ CoreFoundation.CFArrayGetValueAtIndex.argtypes = [
489
+ CFArrayRef,
490
+ CFIndex
491
+ ]
492
+ CoreFoundation.CFArrayGetValueAtIndex.restype = c_void_p
493
+
494
+ CoreFoundation.kCFAllocatorDefault = CFAllocatorRef.in_dll(
495
+ CoreFoundation, 'kCFAllocatorDefault'
496
+ )
497
+ CoreFoundation.kCFTypeArrayCallBacks = c_void_p.in_dll(CoreFoundation, 'kCFTypeArrayCallBacks')
498
+ CoreFoundation.kCFTypeDictionaryKeyCallBacks = c_void_p.in_dll(
499
+ CoreFoundation, 'kCFTypeDictionaryKeyCallBacks'
500
+ )
501
+ CoreFoundation.kCFTypeDictionaryValueCallBacks = c_void_p.in_dll(
502
+ CoreFoundation, 'kCFTypeDictionaryValueCallBacks'
503
+ )
504
+
505
+ CoreFoundation.CFTypeRef = CFTypeRef
506
+ CoreFoundation.CFArrayRef = CFArrayRef
507
+ CoreFoundation.CFStringRef = CFStringRef
508
+ CoreFoundation.CFDictionaryRef = CFDictionaryRef
509
+
510
+ except (AttributeError):
511
+ raise ImportError('Error initializing ctypes')
512
+
513
+
514
+ class CFConst(object):
515
+ """
516
+ A class object that acts as essentially a namespace for CoreFoundation
517
+ constants.
518
+ """
519
+ kCFStringEncodingUTF8 = CFStringEncoding(0x08000100)
520
+
521
+
522
+ class SecurityConst(object):
523
+ """
524
+ A class object that acts as essentially a namespace for Security constants.
525
+ """
526
+ kSSLSessionOptionBreakOnServerAuth = 0
527
+
528
+ kSSLProtocol2 = 1
529
+ kSSLProtocol3 = 2
530
+ kTLSProtocol1 = 4
531
+ kTLSProtocol11 = 7
532
+ kTLSProtocol12 = 8
533
+
534
+ kSSLClientSide = 1
535
+ kSSLStreamType = 0
536
+
537
+ kSecFormatPEMSequence = 10
538
+
539
+ kSecTrustResultInvalid = 0
540
+ kSecTrustResultProceed = 1
541
+ # This gap is present on purpose: this was kSecTrustResultConfirm, which
542
+ # is deprecated.
543
+ kSecTrustResultDeny = 3
544
+ kSecTrustResultUnspecified = 4
545
+ kSecTrustResultRecoverableTrustFailure = 5
546
+ kSecTrustResultFatalTrustFailure = 6
547
+ kSecTrustResultOtherError = 7
548
+
549
+ errSSLProtocol = -9800
550
+ errSSLWouldBlock = -9803
551
+ errSSLClosedGraceful = -9805
552
+ errSSLClosedNoNotify = -9816
553
+ errSSLClosedAbort = -9806
554
+
555
+ errSSLXCertChainInvalid = -9807
556
+ errSSLCrypto = -9809
557
+ errSSLInternal = -9810
558
+ errSSLCertExpired = -9814
559
+ errSSLCertNotYetValid = -9815
560
+ errSSLUnknownRootCert = -9812
561
+ errSSLNoRootCert = -9813
562
+ errSSLHostNameMismatch = -9843
563
+ errSSLPeerHandshakeFail = -9824
564
+ errSSLPeerUserCancelled = -9839
565
+ errSSLWeakPeerEphemeralDHKey = -9850
566
+ errSSLServerAuthCompleted = -9841
567
+ errSSLRecordOverflow = -9847
568
+
569
+ errSecVerifyFailed = -67808
570
+ errSecNoTrustSettings = -25263
571
+ errSecItemNotFound = -25300
572
+ errSecInvalidTrustSettings = -25262
573
+
574
+ # Cipher suites. We only pick the ones our default cipher string allows.
575
+ TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 = 0xC02C
576
+ TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0xC030
577
+ TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0xC02B
578
+ TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xC02F
579
+ TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 = 0x00A3
580
+ TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 = 0x009F
581
+ TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 = 0x00A2
582
+ TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 = 0x009E
583
+ TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = 0xC024
584
+ TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 = 0xC028
585
+ TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA = 0xC00A
586
+ TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA = 0xC014
587
+ TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = 0x006B
588
+ TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 = 0x006A
589
+ TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 0x0039
590
+ TLS_DHE_DSS_WITH_AES_256_CBC_SHA = 0x0038
591
+ TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 = 0xC023
592
+ TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 = 0xC027
593
+ TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA = 0xC009
594
+ TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA = 0xC013
595
+ TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = 0x0067
596
+ TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 = 0x0040
597
+ TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 0x0033
598
+ TLS_DHE_DSS_WITH_AES_128_CBC_SHA = 0x0032
599
+ TLS_RSA_WITH_AES_256_GCM_SHA384 = 0x009D
600
+ TLS_RSA_WITH_AES_128_GCM_SHA256 = 0x009C
601
+ TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x003D
602
+ TLS_RSA_WITH_AES_128_CBC_SHA256 = 0x003C
603
+ TLS_RSA_WITH_AES_256_CBC_SHA = 0x0035
604
+ TLS_RSA_WITH_AES_128_CBC_SHA = 0x002F
605
+ TLS_AES_128_GCM_SHA256 = 0x1301
606
+ TLS_AES_256_GCM_SHA384 = 0x1302
607
+ TLS_CHACHA20_POLY1305_SHA256 = 0x1303