m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

430 lines
14 KiB

6 months ago
  1. # type: ignore
  2. """
  3. This module uses ctypes to bind a whole bunch of functions and constants from
  4. SecureTransport. The goal here is to provide the low-level API to
  5. SecureTransport. These are essentially the C-level functions and constants, and
  6. they're pretty gross to work with.
  7. This code is a bastardised version of the code found in Will Bond's oscrypto
  8. library. An enormous debt is owed to him for blazing this trail for us. For
  9. that reason, this code should be considered to be covered both by urllib3's
  10. license and by oscrypto's:
  11. Copyright (c) 2015-2016 Will Bond <will@wbond.net>
  12. Permission is hereby granted, free of charge, to any person obtaining a
  13. copy of this software and associated documentation files (the "Software"),
  14. to deal in the Software without restriction, including without limitation
  15. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. and/or sell copies of the Software, and to permit persons to whom the
  17. Software is furnished to do so, subject to the following conditions:
  18. The above copyright notice and this permission notice shall be included in
  19. all copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. DEALINGS IN THE SOFTWARE.
  27. """
  28. from __future__ import annotations
  29. import platform
  30. from ctypes import (
  31. CDLL,
  32. CFUNCTYPE,
  33. POINTER,
  34. c_bool,
  35. c_byte,
  36. c_char_p,
  37. c_int32,
  38. c_long,
  39. c_size_t,
  40. c_uint32,
  41. c_ulong,
  42. c_void_p,
  43. )
  44. from ctypes.util import find_library
  45. if platform.system() != "Darwin":
  46. raise ImportError("Only macOS is supported")
  47. version = platform.mac_ver()[0]
  48. version_info = tuple(map(int, version.split(".")))
  49. if version_info < (10, 8):
  50. raise OSError(
  51. f"Only OS X 10.8 and newer are supported, not {version_info[0]}.{version_info[1]}"
  52. )
  53. def load_cdll(name: str, macos10_16_path: str) -> CDLL:
  54. """Loads a CDLL by name, falling back to known path on 10.16+"""
  55. try:
  56. # Big Sur is technically 11 but we use 10.16 due to the Big Sur
  57. # beta being labeled as 10.16.
  58. path: str | None
  59. if version_info >= (10, 16):
  60. path = macos10_16_path
  61. else:
  62. path = find_library(name)
  63. if not path:
  64. raise OSError # Caught and reraised as 'ImportError'
  65. return CDLL(path, use_errno=True)
  66. except OSError:
  67. raise ImportError(f"The library {name} failed to load") from None
  68. Security = load_cdll(
  69. "Security", "/System/Library/Frameworks/Security.framework/Security"
  70. )
  71. CoreFoundation = load_cdll(
  72. "CoreFoundation",
  73. "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation",
  74. )
  75. Boolean = c_bool
  76. CFIndex = c_long
  77. CFStringEncoding = c_uint32
  78. CFData = c_void_p
  79. CFString = c_void_p
  80. CFArray = c_void_p
  81. CFMutableArray = c_void_p
  82. CFDictionary = c_void_p
  83. CFError = c_void_p
  84. CFType = c_void_p
  85. CFTypeID = c_ulong
  86. CFTypeRef = POINTER(CFType)
  87. CFAllocatorRef = c_void_p
  88. OSStatus = c_int32
  89. CFDataRef = POINTER(CFData)
  90. CFStringRef = POINTER(CFString)
  91. CFArrayRef = POINTER(CFArray)
  92. CFMutableArrayRef = POINTER(CFMutableArray)
  93. CFDictionaryRef = POINTER(CFDictionary)
  94. CFArrayCallBacks = c_void_p
  95. CFDictionaryKeyCallBacks = c_void_p
  96. CFDictionaryValueCallBacks = c_void_p
  97. SecCertificateRef = POINTER(c_void_p)
  98. SecExternalFormat = c_uint32
  99. SecExternalItemType = c_uint32
  100. SecIdentityRef = POINTER(c_void_p)
  101. SecItemImportExportFlags = c_uint32
  102. SecItemImportExportKeyParameters = c_void_p
  103. SecKeychainRef = POINTER(c_void_p)
  104. SSLProtocol = c_uint32
  105. SSLCipherSuite = c_uint32
  106. SSLContextRef = POINTER(c_void_p)
  107. SecTrustRef = POINTER(c_void_p)
  108. SSLConnectionRef = c_uint32
  109. SecTrustResultType = c_uint32
  110. SecTrustOptionFlags = c_uint32
  111. SSLProtocolSide = c_uint32
  112. SSLConnectionType = c_uint32
  113. SSLSessionOption = c_uint32
  114. try:
  115. Security.SecItemImport.argtypes = [
  116. CFDataRef,
  117. CFStringRef,
  118. POINTER(SecExternalFormat),
  119. POINTER(SecExternalItemType),
  120. SecItemImportExportFlags,
  121. POINTER(SecItemImportExportKeyParameters),
  122. SecKeychainRef,
  123. POINTER(CFArrayRef),
  124. ]
  125. Security.SecItemImport.restype = OSStatus
  126. Security.SecCertificateGetTypeID.argtypes = []
  127. Security.SecCertificateGetTypeID.restype = CFTypeID
  128. Security.SecIdentityGetTypeID.argtypes = []
  129. Security.SecIdentityGetTypeID.restype = CFTypeID
  130. Security.SecKeyGetTypeID.argtypes = []
  131. Security.SecKeyGetTypeID.restype = CFTypeID
  132. Security.SecCertificateCreateWithData.argtypes = [CFAllocatorRef, CFDataRef]
  133. Security.SecCertificateCreateWithData.restype = SecCertificateRef
  134. Security.SecCertificateCopyData.argtypes = [SecCertificateRef]
  135. Security.SecCertificateCopyData.restype = CFDataRef
  136. Security.SecCopyErrorMessageString.argtypes = [OSStatus, c_void_p]
  137. Security.SecCopyErrorMessageString.restype = CFStringRef
  138. Security.SecIdentityCreateWithCertificate.argtypes = [
  139. CFTypeRef,
  140. SecCertificateRef,
  141. POINTER(SecIdentityRef),
  142. ]
  143. Security.SecIdentityCreateWithCertificate.restype = OSStatus
  144. Security.SecKeychainCreate.argtypes = [
  145. c_char_p,
  146. c_uint32,
  147. c_void_p,
  148. Boolean,
  149. c_void_p,
  150. POINTER(SecKeychainRef),
  151. ]
  152. Security.SecKeychainCreate.restype = OSStatus
  153. Security.SecKeychainDelete.argtypes = [SecKeychainRef]
  154. Security.SecKeychainDelete.restype = OSStatus
  155. Security.SecPKCS12Import.argtypes = [
  156. CFDataRef,
  157. CFDictionaryRef,
  158. POINTER(CFArrayRef),
  159. ]
  160. Security.SecPKCS12Import.restype = OSStatus
  161. SSLReadFunc = CFUNCTYPE(OSStatus, SSLConnectionRef, c_void_p, POINTER(c_size_t))
  162. SSLWriteFunc = CFUNCTYPE(
  163. OSStatus, SSLConnectionRef, POINTER(c_byte), POINTER(c_size_t)
  164. )
  165. Security.SSLSetIOFuncs.argtypes = [SSLContextRef, SSLReadFunc, SSLWriteFunc]
  166. Security.SSLSetIOFuncs.restype = OSStatus
  167. Security.SSLSetPeerID.argtypes = [SSLContextRef, c_char_p, c_size_t]
  168. Security.SSLSetPeerID.restype = OSStatus
  169. Security.SSLSetCertificate.argtypes = [SSLContextRef, CFArrayRef]
  170. Security.SSLSetCertificate.restype = OSStatus
  171. Security.SSLSetCertificateAuthorities.argtypes = [SSLContextRef, CFTypeRef, Boolean]
  172. Security.SSLSetCertificateAuthorities.restype = OSStatus
  173. Security.SSLSetConnection.argtypes = [SSLContextRef, SSLConnectionRef]
  174. Security.SSLSetConnection.restype = OSStatus
  175. Security.SSLSetPeerDomainName.argtypes = [SSLContextRef, c_char_p, c_size_t]
  176. Security.SSLSetPeerDomainName.restype = OSStatus
  177. Security.SSLHandshake.argtypes = [SSLContextRef]
  178. Security.SSLHandshake.restype = OSStatus
  179. Security.SSLRead.argtypes = [SSLContextRef, c_char_p, c_size_t, POINTER(c_size_t)]
  180. Security.SSLRead.restype = OSStatus
  181. Security.SSLWrite.argtypes = [SSLContextRef, c_char_p, c_size_t, POINTER(c_size_t)]
  182. Security.SSLWrite.restype = OSStatus
  183. Security.SSLClose.argtypes = [SSLContextRef]
  184. Security.SSLClose.restype = OSStatus
  185. Security.SSLGetNumberSupportedCiphers.argtypes = [SSLContextRef, POINTER(c_size_t)]
  186. Security.SSLGetNumberSupportedCiphers.restype = OSStatus
  187. Security.SSLGetSupportedCiphers.argtypes = [
  188. SSLContextRef,
  189. POINTER(SSLCipherSuite),
  190. POINTER(c_size_t),
  191. ]
  192. Security.SSLGetSupportedCiphers.restype = OSStatus
  193. Security.SSLSetEnabledCiphers.argtypes = [
  194. SSLContextRef,
  195. POINTER(SSLCipherSuite),
  196. c_size_t,
  197. ]
  198. Security.SSLSetEnabledCiphers.restype = OSStatus
  199. Security.SSLGetNumberEnabledCiphers.argtype = [SSLContextRef, POINTER(c_size_t)]
  200. Security.SSLGetNumberEnabledCiphers.restype = OSStatus
  201. Security.SSLGetEnabledCiphers.argtypes = [
  202. SSLContextRef,
  203. POINTER(SSLCipherSuite),
  204. POINTER(c_size_t),
  205. ]
  206. Security.SSLGetEnabledCiphers.restype = OSStatus
  207. Security.SSLGetNegotiatedCipher.argtypes = [SSLContextRef, POINTER(SSLCipherSuite)]
  208. Security.SSLGetNegotiatedCipher.restype = OSStatus
  209. Security.SSLGetNegotiatedProtocolVersion.argtypes = [
  210. SSLContextRef,
  211. POINTER(SSLProtocol),
  212. ]
  213. Security.SSLGetNegotiatedProtocolVersion.restype = OSStatus
  214. Security.SSLCopyPeerTrust.argtypes = [SSLContextRef, POINTER(SecTrustRef)]
  215. Security.SSLCopyPeerTrust.restype = OSStatus
  216. Security.SecTrustSetAnchorCertificates.argtypes = [SecTrustRef, CFArrayRef]
  217. Security.SecTrustSetAnchorCertificates.restype = OSStatus
  218. Security.SecTrustSetAnchorCertificatesOnly.argstypes = [SecTrustRef, Boolean]
  219. Security.SecTrustSetAnchorCertificatesOnly.restype = OSStatus
  220. Security.SecTrustEvaluate.argtypes = [SecTrustRef, POINTER(SecTrustResultType)]
  221. Security.SecTrustEvaluate.restype = OSStatus
  222. Security.SecTrustGetCertificateCount.argtypes = [SecTrustRef]
  223. Security.SecTrustGetCertificateCount.restype = CFIndex
  224. Security.SecTrustGetCertificateAtIndex.argtypes = [SecTrustRef, CFIndex]
  225. Security.SecTrustGetCertificateAtIndex.restype = SecCertificateRef
  226. Security.SSLCreateContext.argtypes = [
  227. CFAllocatorRef,
  228. SSLProtocolSide,
  229. SSLConnectionType,
  230. ]
  231. Security.SSLCreateContext.restype = SSLContextRef
  232. Security.SSLSetSessionOption.argtypes = [SSLContextRef, SSLSessionOption, Boolean]
  233. Security.SSLSetSessionOption.restype = OSStatus
  234. Security.SSLSetProtocolVersionMin.argtypes = [SSLContextRef, SSLProtocol]
  235. Security.SSLSetProtocolVersionMin.restype = OSStatus
  236. Security.SSLSetProtocolVersionMax.argtypes = [SSLContextRef, SSLProtocol]
  237. Security.SSLSetProtocolVersionMax.restype = OSStatus
  238. try:
  239. Security.SSLSetALPNProtocols.argtypes = [SSLContextRef, CFArrayRef]
  240. Security.SSLSetALPNProtocols.restype = OSStatus
  241. except AttributeError:
  242. # Supported only in 10.12+
  243. pass
  244. Security.SecCopyErrorMessageString.argtypes = [OSStatus, c_void_p]
  245. Security.SecCopyErrorMessageString.restype = CFStringRef
  246. Security.SSLReadFunc = SSLReadFunc
  247. Security.SSLWriteFunc = SSLWriteFunc
  248. Security.SSLContextRef = SSLContextRef
  249. Security.SSLProtocol = SSLProtocol
  250. Security.SSLCipherSuite = SSLCipherSuite
  251. Security.SecIdentityRef = SecIdentityRef
  252. Security.SecKeychainRef = SecKeychainRef
  253. Security.SecTrustRef = SecTrustRef
  254. Security.SecTrustResultType = SecTrustResultType
  255. Security.SecExternalFormat = SecExternalFormat
  256. Security.OSStatus = OSStatus
  257. Security.kSecImportExportPassphrase = CFStringRef.in_dll(
  258. Security, "kSecImportExportPassphrase"
  259. )
  260. Security.kSecImportItemIdentity = CFStringRef.in_dll(
  261. Security, "kSecImportItemIdentity"
  262. )
  263. # CoreFoundation time!
  264. CoreFoundation.CFRetain.argtypes = [CFTypeRef]
  265. CoreFoundation.CFRetain.restype = CFTypeRef
  266. CoreFoundation.CFRelease.argtypes = [CFTypeRef]
  267. CoreFoundation.CFRelease.restype = None
  268. CoreFoundation.CFGetTypeID.argtypes = [CFTypeRef]
  269. CoreFoundation.CFGetTypeID.restype = CFTypeID
  270. CoreFoundation.CFStringCreateWithCString.argtypes = [
  271. CFAllocatorRef,
  272. c_char_p,
  273. CFStringEncoding,
  274. ]
  275. CoreFoundation.CFStringCreateWithCString.restype = CFStringRef
  276. CoreFoundation.CFStringGetCStringPtr.argtypes = [CFStringRef, CFStringEncoding]
  277. CoreFoundation.CFStringGetCStringPtr.restype = c_char_p
  278. CoreFoundation.CFStringGetCString.argtypes = [
  279. CFStringRef,
  280. c_char_p,
  281. CFIndex,
  282. CFStringEncoding,
  283. ]
  284. CoreFoundation.CFStringGetCString.restype = c_bool
  285. CoreFoundation.CFDataCreate.argtypes = [CFAllocatorRef, c_char_p, CFIndex]
  286. CoreFoundation.CFDataCreate.restype = CFDataRef
  287. CoreFoundation.CFDataGetLength.argtypes = [CFDataRef]
  288. CoreFoundation.CFDataGetLength.restype = CFIndex
  289. CoreFoundation.CFDataGetBytePtr.argtypes = [CFDataRef]
  290. CoreFoundation.CFDataGetBytePtr.restype = c_void_p
  291. CoreFoundation.CFDictionaryCreate.argtypes = [
  292. CFAllocatorRef,
  293. POINTER(CFTypeRef),
  294. POINTER(CFTypeRef),
  295. CFIndex,
  296. CFDictionaryKeyCallBacks,
  297. CFDictionaryValueCallBacks,
  298. ]
  299. CoreFoundation.CFDictionaryCreate.restype = CFDictionaryRef
  300. CoreFoundation.CFDictionaryGetValue.argtypes = [CFDictionaryRef, CFTypeRef]
  301. CoreFoundation.CFDictionaryGetValue.restype = CFTypeRef
  302. CoreFoundation.CFArrayCreate.argtypes = [
  303. CFAllocatorRef,
  304. POINTER(CFTypeRef),
  305. CFIndex,
  306. CFArrayCallBacks,
  307. ]
  308. CoreFoundation.CFArrayCreate.restype = CFArrayRef
  309. CoreFoundation.CFArrayCreateMutable.argtypes = [
  310. CFAllocatorRef,
  311. CFIndex,
  312. CFArrayCallBacks,
  313. ]
  314. CoreFoundation.CFArrayCreateMutable.restype = CFMutableArrayRef
  315. CoreFoundation.CFArrayAppendValue.argtypes = [CFMutableArrayRef, c_void_p]
  316. CoreFoundation.CFArrayAppendValue.restype = None
  317. CoreFoundation.CFArrayGetCount.argtypes = [CFArrayRef]
  318. CoreFoundation.CFArrayGetCount.restype = CFIndex
  319. CoreFoundation.CFArrayGetValueAtIndex.argtypes = [CFArrayRef, CFIndex]
  320. CoreFoundation.CFArrayGetValueAtIndex.restype = c_void_p
  321. CoreFoundation.kCFAllocatorDefault = CFAllocatorRef.in_dll(
  322. CoreFoundation, "kCFAllocatorDefault"
  323. )
  324. CoreFoundation.kCFTypeArrayCallBacks = c_void_p.in_dll(
  325. CoreFoundation, "kCFTypeArrayCallBacks"
  326. )
  327. CoreFoundation.kCFTypeDictionaryKeyCallBacks = c_void_p.in_dll(
  328. CoreFoundation, "kCFTypeDictionaryKeyCallBacks"
  329. )
  330. CoreFoundation.kCFTypeDictionaryValueCallBacks = c_void_p.in_dll(
  331. CoreFoundation, "kCFTypeDictionaryValueCallBacks"
  332. )
  333. CoreFoundation.CFTypeRef = CFTypeRef
  334. CoreFoundation.CFArrayRef = CFArrayRef
  335. CoreFoundation.CFStringRef = CFStringRef
  336. CoreFoundation.CFDictionaryRef = CFDictionaryRef
  337. except AttributeError:
  338. raise ImportError("Error initializing ctypes") from None
  339. class CFConst:
  340. """
  341. A class object that acts as essentially a namespace for CoreFoundation
  342. constants.
  343. """
  344. kCFStringEncodingUTF8 = CFStringEncoding(0x08000100)