2This module uses ctypes to bind a whole bunch of functions and constants from
3SecureTransport. The goal here is to provide the low-level API to
4SecureTransport. These are essentially the C-level functions and constants, and
5they're pretty gross to work with.
7This code is a bastardised version of the code found in Will Bond's oscrypto
8library. An enormous debt is owed to him for blazing this trail for us. For
9that reason, this code should be considered to be covered both by urllib3's
10license and by oscrypto's:
12 Copyright (c) 2015-2016 Will Bond <will@wbond.net>
14 Permission is hereby granted, free of charge, to any person obtaining a
15 copy of this software and associated documentation files (the "Software"),
16 to deal in the Software without restriction, including without limitation
17 the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 and/or sell copies of the Software, and to permit persons to whom the
19 Software is furnished to do so, subject to the following conditions:
21 The above copyright notice and this permission notice shall be included in
22 all copies or substantial portions of the Software.
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 DEALINGS IN THE SOFTWARE.
32from __future__
import absolute_import
54 raise ImportError(
"Only macOS is supported")
58if version_info < (10, 8):
60 "Only OS X 10.8 and newer are supported, not %s.%s"
61 % (version_info[0], version_info[1])
66 """Loads a CDLL by name, falling back to known path on 10.16+"""
70 if version_info >= (10, 16):
71 path = macos10_16_path
73 path = find_library(name)
76 return CDLL(path, use_errno=
True)
78 raise_from(ImportError(
"The library %s failed to load" % name),
None)
82 "Security",
"/System/Library/Frameworks/Security.framework/Security"
86 "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation",
92CFStringEncoding = c_uint32
96CFMutableArray = c_void_p
97CFDictionary = c_void_p
102CFTypeRef = POINTER(CFType)
103CFAllocatorRef = c_void_p
107CFDataRef = POINTER(CFData)
108CFStringRef = POINTER(CFString)
109CFArrayRef = POINTER(CFArray)
110CFMutableArrayRef = POINTER(CFMutableArray)
111CFDictionaryRef = POINTER(CFDictionary)
112CFArrayCallBacks = c_void_p
113CFDictionaryKeyCallBacks = c_void_p
114CFDictionaryValueCallBacks = c_void_p
116SecCertificateRef = POINTER(c_void_p)
117SecExternalFormat = c_uint32
118SecExternalItemType = c_uint32
119SecIdentityRef = POINTER(c_void_p)
120SecItemImportExportFlags = c_uint32
121SecItemImportExportKeyParameters = c_void_p
122SecKeychainRef = POINTER(c_void_p)
123SSLProtocol = c_uint32
124SSLCipherSuite = c_uint32
125SSLContextRef = POINTER(c_void_p)
126SecTrustRef = POINTER(c_void_p)
127SSLConnectionRef = c_uint32
128SecTrustResultType = c_uint32
129SecTrustOptionFlags = c_uint32
130SSLProtocolSide = c_uint32
131SSLConnectionType = c_uint32
132SSLSessionOption = c_uint32
139 POINTER(SecExternalFormat),
140 POINTER(SecExternalItemType),
141 SecItemImportExportFlags,
142 POINTER(SecItemImportExportKeyParameters),
169 POINTER(SecIdentityRef),
179 POINTER(SecKeychainRef),
193 SSLReadFunc = CFUNCTYPE(OSStatus, SSLConnectionRef, c_void_p, POINTER(c_size_t))
194 SSLWriteFunc = CFUNCTYPE(
195 OSStatus, SSLConnectionRef, POINTER(c_byte), POINTER(c_size_t)
233 POINTER(SSLCipherSuite),
240 POINTER(SSLCipherSuite),
250 POINTER(SSLCipherSuite),
260 POINTER(SSLProtocol),
301 except AttributeError:
321 Security,
"kSecImportExportPassphrase"
324 Security,
"kSecImportItemIdentity"
369 CFDictionaryKeyCallBacks,
370 CFDictionaryValueCallBacks,
402 CoreFoundation,
"kCFAllocatorDefault"
405 CoreFoundation,
"kCFTypeArrayCallBacks"
408 CoreFoundation,
"kCFTypeDictionaryKeyCallBacks"
411 CoreFoundation,
"kCFTypeDictionaryValueCallBacks"
419except (AttributeError):
420 raise ImportError(
"Error initializing ctypes")
425 A class object that acts as essentially a namespace for CoreFoundation
434 A class object that acts as essentially a namespace for Security constants.
437 kSSLSessionOptionBreakOnServerAuth = 0
446 kTLSProtocolMaxSupported = 999
451 kSecFormatPEMSequence = 10
453 kSecTrustResultInvalid = 0
454 kSecTrustResultProceed = 1
457 kSecTrustResultDeny = 3
458 kSecTrustResultUnspecified = 4
459 kSecTrustResultRecoverableTrustFailure = 5
460 kSecTrustResultFatalTrustFailure = 6
461 kSecTrustResultOtherError = 7
463 errSSLProtocol = -9800
464 errSSLWouldBlock = -9803
465 errSSLClosedGraceful = -9805
466 errSSLClosedNoNotify = -9816
467 errSSLClosedAbort = -9806
469 errSSLXCertChainInvalid = -9807
471 errSSLInternal = -9810
472 errSSLCertExpired = -9814
473 errSSLCertNotYetValid = -9815
474 errSSLUnknownRootCert = -9812
475 errSSLNoRootCert = -9813
476 errSSLHostNameMismatch = -9843
477 errSSLPeerHandshakeFail = -9824
478 errSSLPeerUserCancelled = -9839
479 errSSLWeakPeerEphemeralDHKey = -9850
480 errSSLServerAuthCompleted = -9841
481 errSSLRecordOverflow = -9847
483 errSecVerifyFailed = -67808
484 errSecNoTrustSettings = -25263
485 errSecItemNotFound = -25300
486 errSecInvalidTrustSettings = -25262
490 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 = 0xC02C
491 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0xC030
492 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0xC02B
493 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xC02F
494 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = 0xCCA9
495 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = 0xCCA8
496 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 = 0x009F
497 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 = 0x009E
498 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = 0xC024
499 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 = 0xC028
500 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA = 0xC00A
501 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA = 0xC014
502 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = 0x006B
503 TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 0x0039
504 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 = 0xC023
505 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 = 0xC027
506 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA = 0xC009
507 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA = 0xC013
508 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = 0x0067
509 TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 0x0033
510 TLS_RSA_WITH_AES_256_GCM_SHA384 = 0x009D
511 TLS_RSA_WITH_AES_128_GCM_SHA256 = 0x009C
512 TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x003D
513 TLS_RSA_WITH_AES_128_CBC_SHA256 = 0x003C
514 TLS_RSA_WITH_AES_256_CBC_SHA = 0x0035
515 TLS_RSA_WITH_AES_128_CBC_SHA = 0x002F
516 TLS_AES_128_GCM_SHA256 = 0x1301
517 TLS_AES_256_GCM_SHA384 = 0x1302
518 TLS_AES_128_CCM_8_SHA256 = 0x1305
519 TLS_AES_128_CCM_SHA256 = 0x1304
load_cdll(name, macos10_16_path)