-
Notifications
You must be signed in to change notification settings - Fork 109
/
CFSocketStream.c
259 lines (216 loc) · 11.3 KB
/
CFSocketStream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (c) 2015 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFSocketStream.c
Copyright (c) 2000-2014, Apple Inc. All rights reserved.
Responsibility: Jeremy Wyld
*/
// Original Author: Becky Willrich
#include <CoreFoundation/CFStream.h>
#include <CoreFoundation/CFNumber.h>
#include "CFInternal.h"
#include "CFStreamInternal.h"
#include "CFStreamPriv.h"
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
// On Mach these live in CF for historical reasons, even though they are declared in CFNetwork
const int kCFStreamErrorDomainSSL = 3;
const int kCFStreamErrorDomainSOCKS = 5;
CONST_STRING_DECL(kCFStreamPropertyShouldCloseNativeSocket, "kCFStreamPropertyShouldCloseNativeSocket")
CONST_STRING_DECL(kCFStreamPropertyAutoErrorOnSystemChange, "kCFStreamPropertyAutoErrorOnSystemChange");
CONST_STRING_DECL(kCFStreamPropertySOCKSProxy, "kCFStreamPropertySOCKSProxy")
CONST_STRING_DECL(kCFStreamPropertySOCKSProxyHost, "SOCKSProxy")
CONST_STRING_DECL(kCFStreamPropertySOCKSProxyPort, "SOCKSPort")
CONST_STRING_DECL(kCFStreamPropertySOCKSVersion, "kCFStreamPropertySOCKSVersion")
CONST_STRING_DECL(kCFStreamSocketSOCKSVersion4, "kCFStreamSocketSOCKSVersion4")
CONST_STRING_DECL(kCFStreamSocketSOCKSVersion5, "kCFStreamSocketSOCKSVersion5")
CONST_STRING_DECL(kCFStreamPropertySOCKSUser, "kCFStreamPropertySOCKSUser")
CONST_STRING_DECL(kCFStreamPropertySOCKSPassword, "kCFStreamPropertySOCKSPassword")
CONST_STRING_DECL(kCFStreamPropertySocketSecurityLevel, "kCFStreamPropertySocketSecurityLevel");
CONST_STRING_DECL(kCFStreamSocketSecurityLevelNone, "kCFStreamSocketSecurityLevelNone");
CONST_STRING_DECL(kCFStreamSocketSecurityLevelSSLv2, "kCFStreamSocketSecurityLevelSSLv2");
CONST_STRING_DECL(kCFStreamSocketSecurityLevelSSLv3, "kCFStreamSocketSecurityLevelSSLv3");
CONST_STRING_DECL(kCFStreamSocketSecurityLevelTLSv1, "kCFStreamSocketSecurityLevelTLSv1");
CONST_STRING_DECL(kCFStreamSocketSecurityLevelNegotiatedSSL, "kCFStreamSocketSecurityLevelNegotiatedSSL");
#endif
#if DEPLOYMENT_TARGET_WINDOWS
typedef void (*CF_SOCKET_STREAM_PAIR)(CFAllocatorRef, CFStringRef, UInt32, CFSocketNativeHandle, const CFSocketSignature*, CFReadStreamRef*, CFWriteStreamRef*);
#endif
// These are duplicated in CFNetwork, who actually externs them in its headers
CONST_STRING_DECL(kCFStreamPropertySocketSSLContext, "kCFStreamPropertySocketSSLContext")
CONST_STRING_DECL(_kCFStreamPropertySocketSecurityAuthenticatesServerCertificate, "_kCFStreamPropertySocketSecurityAuthenticatesServerCertificate");
CF_EXPORT
void _CFSocketStreamSetAuthenticatesServerCertificateDefault(Boolean shouldAuthenticate) {
CFLog(__kCFLogAssertion, CFSTR("_CFSocketStreamSetAuthenticatesServerCertificateDefault(): This call has been deprecated. Use SetProperty(_kCFStreamPropertySocketSecurityAuthenticatesServerCertificate, kCFBooleanTrue/False)\n"));
}
/* CF_EXPORT */ Boolean
_CFSocketStreamGetAuthenticatesServerCertificateDefault(void) {
CFLog(__kCFLogAssertion, CFSTR("_CFSocketStreamGetAuthenticatesServerCertificateDefault(): This call has been removed as a security risk. Use security properties on individual streams instead.\n"));
return FALSE;
}
/* CF_EXPORT */ void
_CFSocketStreamPairSetAuthenticatesServerCertificate(CFReadStreamRef rStream, CFWriteStreamRef wStream, Boolean authenticates) {
CFBooleanRef value = (!authenticates ? kCFBooleanFalse : kCFBooleanTrue);
if (rStream)
CFReadStreamSetProperty(rStream, _kCFStreamPropertySocketSecurityAuthenticatesServerCertificate, value);
else
CFWriteStreamSetProperty(wStream, _kCFStreamPropertySocketSecurityAuthenticatesServerCertificate, value);
}
// Flags for dyld loading of libraries.
enum {
kTriedToLoad = 0,
kInitialized
};
static struct {
CFLock_t lock;
UInt32 flags;
#if DEPLOYMENT_TARGET_WINDOWS
HMODULE image;
#endif
void (*_CFSocketStreamCreatePair)(CFAllocatorRef, CFStringRef, UInt32, CFSocketNativeHandle, const CFSocketSignature*, CFReadStreamRef*, CFWriteStreamRef*);
CFErrorRef (*_CFErrorCreateWithStreamError)(CFAllocatorRef, CFStreamError*);
CFStreamError (*_CFStreamErrorFromCFError)(CFErrorRef);
} CFNetworkSupport = {
CFLockInit,
0x0,
#if DEPLOYMENT_TARGET_WINDOWS
NULL,
#endif
NULL,
NULL,
NULL
};
#define CFNETWORK_CALL(sym, args) ((CFNetworkSupport.sym)args)
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
#define CFNETWORK_LOAD_SYM(sym) __CFLookupCFNetworkFunction(#sym)
#elif DEPLOYMENT_TARGET_WINDOWS
#define CFNETWORK_LOAD_SYM(sym) (void *)GetProcAddress(CFNetworkSupport.image, #sym)
#endif
static void initializeCFNetworkSupport(void) {
__CFBitSet(CFNetworkSupport.flags, kTriedToLoad);
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
CFNetworkSupport._CFSocketStreamCreatePair = CFNETWORK_LOAD_SYM(_CFSocketStreamCreatePair);
CFNetworkSupport._CFErrorCreateWithStreamError = CFNETWORK_LOAD_SYM(_CFErrorCreateWithStreamError);
CFNetworkSupport._CFStreamErrorFromCFError = CFNETWORK_LOAD_SYM(_CFStreamErrorFromCFError);
#elif DEPLOYMENT_TARGET_WINDOWS
if (!CFNetworkSupport.image) {
#if _DEBUG
CFNetworkSupport.image = GetModuleHandleW(L"CFNetwork_debug.dll");
#else
CFNetworkSupport.image = GetModuleHandleW(L"CFNetwork.dll");
#endif
}
if (!CFNetworkSupport.image) {
// not loaded yet, try to load from the filesystem
char path[MAX_PATH+1];
if (!CFNetworkSupport.image) {
strlcpy(path, (const char *)_CFDLLPath(), sizeof(path));
#if _DEBUG
strlcat(path, "\\CFNetwork_debug.dll", sizeof(path));
#else
strlcat(path, "\\CFNetwork.dll", sizeof(path));
#endif
CFNetworkSupport.image = LoadLibraryA(path);
}
}
if (!CFNetworkSupport.image) {
CFLog(__kCFLogAssertion, CFSTR("CoreFoundation: failed to dynamically load CFNetwork"));
} else {
CFNetworkSupport._CFSocketStreamCreatePair = (CF_SOCKET_STREAM_PAIR)CFNETWORK_LOAD_SYM(_CFSocketStreamCreatePair);
CFNetworkSupport._CFErrorCreateWithStreamError = (CFErrorRef(*)(CFAllocatorRef, CFStreamError *))CFNETWORK_LOAD_SYM(_CFErrorCreateWithStreamError);
CFNetworkSupport._CFStreamErrorFromCFError = (CFStreamError(*)(CFErrorRef))CFNETWORK_LOAD_SYM(_CFStreamErrorFromCFError);
}
#endif
if (!CFNetworkSupport._CFSocketStreamCreatePair) CFLog(__kCFLogAssertion, CFSTR("CoreFoundation: failed to dynamically link symbol _CFSocketStreamCreatePair"));
if (!CFNetworkSupport._CFErrorCreateWithStreamError) CFLog(__kCFLogAssertion, CFSTR("CoreFoundation: failed to dynamically link symbol _CFErrorCreateWithStreamError"));
if (!CFNetworkSupport._CFStreamErrorFromCFError) CFLog(__kCFLogAssertion, CFSTR("CoreFoundation: failed to dynamically link symbol _CFStreamErrorFromCFError"));
__CFBitSet(CFNetworkSupport.flags, kInitialized);
}
static void
createPair(CFAllocatorRef alloc, CFStringRef host, UInt32 port, CFSocketNativeHandle sock, const CFSocketSignature* sig, CFReadStreamRef *readStream, CFWriteStreamRef *writeStream)
{
if (readStream)
*readStream = NULL;
if (writeStream)
*writeStream = NULL;
__CFLock(&(CFNetworkSupport.lock));
if (!__CFBitIsSet(CFNetworkSupport.flags, kTriedToLoad)) initializeCFNetworkSupport();
__CFUnlock(&(CFNetworkSupport.lock));
CFNETWORK_CALL(_CFSocketStreamCreatePair, (alloc, host, port, sock, sig, readStream, writeStream));
}
CF_EXPORT void CFStreamCreatePairWithSocket(CFAllocatorRef alloc, CFSocketNativeHandle sock, CFReadStreamRef *readStream, CFWriteStreamRef *writeStream) {
createPair(alloc, NULL, 0, sock, NULL, readStream, writeStream);
}
CF_EXPORT void CFStreamCreatePairWithSocketToHost(CFAllocatorRef alloc, CFStringRef host, UInt32 port, CFReadStreamRef *readStream, CFWriteStreamRef *writeStream) {
createPair(alloc, host, port, 0, NULL, readStream, writeStream);
}
CF_EXPORT void CFStreamCreatePairWithPeerSocketSignature(CFAllocatorRef alloc, const CFSocketSignature* sig, CFReadStreamRef *readStream, CFWriteStreamRef *writeStream) {
createPair(alloc, NULL, 0, 0, sig, readStream, writeStream);
}
CF_PRIVATE CFStreamError _CFStreamErrorFromError(CFErrorRef error) {
CFStreamError result;
Boolean canUpCall;
__CFLock(&(CFNetworkSupport.lock));
if (!__CFBitIsSet(CFNetworkSupport.flags, kTriedToLoad)) initializeCFNetworkSupport();
canUpCall = (CFNetworkSupport._CFStreamErrorFromCFError != NULL);
__CFUnlock(&(CFNetworkSupport.lock));
if (canUpCall) {
result = CFNETWORK_CALL(_CFStreamErrorFromCFError, (error));
} else {
CFStringRef domain = CFErrorGetDomain(error);
if (CFEqual(domain, kCFErrorDomainPOSIX)) {
result.domain = kCFStreamErrorDomainPOSIX;
} else if (CFEqual(domain, kCFErrorDomainOSStatus)) {
result.domain = kCFStreamErrorDomainMacOSStatus;
} else if (CFEqual(domain, kCFErrorDomainMach)) {
result.domain = 11; // kCFStreamErrorDomainMach, but that symbol is in CFNetwork
} else {
result.domain = kCFStreamErrorDomainCustom;
}
result.error = CFErrorGetCode(error);
}
return result;
}
CF_PRIVATE CFErrorRef _CFErrorFromStreamError(CFAllocatorRef alloc, CFStreamError *streamError) {
CFErrorRef result;
Boolean canUpCall;
__CFLock(&(CFNetworkSupport.lock));
if (!__CFBitIsSet(CFNetworkSupport.flags, kTriedToLoad)) initializeCFNetworkSupport();
canUpCall = (CFNetworkSupport._CFErrorCreateWithStreamError != NULL);
__CFUnlock(&(CFNetworkSupport.lock));
if (canUpCall) {
result = CFNETWORK_CALL(_CFErrorCreateWithStreamError, (alloc, streamError));
} else {
if (streamError->domain == kCFStreamErrorDomainPOSIX) {
return CFErrorCreate(alloc, kCFErrorDomainPOSIX, streamError->error, NULL);
} else if (streamError->domain == kCFStreamErrorDomainMacOSStatus) {
return CFErrorCreate(alloc, kCFErrorDomainOSStatus, streamError->error, NULL);
} else {
CFStringRef key = CFSTR("CFStreamErrorDomainKey");
CFNumberRef value = CFNumberCreate(alloc, kCFNumberCFIndexType, &streamError->domain);
CFDictionaryRef dict = CFDictionaryCreate(alloc, (const void **)(&key), (const void **)(&value), 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
result = CFErrorCreate(alloc, CFSTR("BogusCFStreamErrorCompatibilityDomain"), streamError->error, dict);
CFRelease(value);
CFRelease(dict);
}
}
return result;
}