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