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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
|
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice Touch is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include "StreamTransceiver.h"
#include "StreamEndpointI.h"
#include <Ice/Properties.h>
#include <Ice/TraceLevels.h>
#include <Ice/Connection.h>
#include <Ice/LoggerUtil.h>
#include <Ice/Buffer.h>
#include <Ice/Network.h>
#include <IceSSL/ConnectionInfo.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
using namespace std;
using namespace Ice;
using namespace IceInternal;
namespace
{
void selectorReadCallback(CFReadStreamRef, CFStreamEventType event, void* info)
{
SelectorReadyCallback* callback = reinterpret_cast<SelectorReadyCallback*>(info);
switch(event)
{
case kCFStreamEventOpenCompleted:
callback->readyCallback(static_cast<SocketOperation>(SocketOperationConnect | SocketOperationRead));
break;
case kCFStreamEventHasBytesAvailable:
callback->readyCallback(SocketOperationRead);
break;
default:
callback->readyCallback(SocketOperationRead, -1); // Error
break;
}
}
void selectorWriteCallback(CFWriteStreamRef, CFStreamEventType event, void* info)
{
SelectorReadyCallback* callback = reinterpret_cast<SelectorReadyCallback*>(info);
switch(event)
{
case kCFStreamEventOpenCompleted:
callback->readyCallback(static_cast<SocketOperation>(SocketOperationConnect | SocketOperationWrite));
break;
case kCFStreamEventCanAcceptBytes:
callback->readyCallback(SocketOperationWrite);
break;
default:
callback->readyCallback(SocketOperationWrite, -1); // Error
break;
}
}
}
static inline string
fromCFString(CFStringRef ref)
{
const char* s = CFStringGetCStringPtr(ref, kCFStringEncodingUTF8);
if(s)
{
return string(s);
}
// Not great, but is good enough for this purpose.
char buf[1024];
CFStringGetCString(ref, buf, sizeof(buf), kCFStringEncodingUTF8);
return string(buf);
}
IceInternal::NativeInfoPtr
IceObjC::StreamTransceiver::getNativeInfo()
{
return this;
}
void
IceObjC::StreamTransceiver::initStreams(SelectorReadyCallback* callback)
{
CFOptionFlags events;
CFStreamClientContext ctx = { 0, callback, 0, 0, 0 };
events = kCFStreamEventOpenCompleted | kCFStreamEventCanAcceptBytes | kCFStreamEventErrorOccurred |
kCFStreamEventEndEncountered;
CFWriteStreamSetClient(_writeStream, events, selectorWriteCallback, &ctx);
events = kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred |
kCFStreamEventEndEncountered;
CFReadStreamSetClient(_readStream, events, selectorReadCallback, &ctx);
}
SocketOperation
IceObjC::StreamTransceiver::registerWithRunLoop(SocketOperation op)
{
IceUtil::Mutex::Lock sync(_mutex);
SocketOperation readyOp = SocketOperationNone;
if(op & SocketOperationConnect)
{
if(CFWriteStreamGetStatus(_writeStream) != kCFStreamStatusNotOpen ||
CFReadStreamGetStatus(_readStream) != kCFStreamStatusNotOpen)
{
return SocketOperationConnect;
}
_opening = true;
CFWriteStreamScheduleWithRunLoop(_writeStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFReadStreamScheduleWithRunLoop(_readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
_writeStreamRegistered = true; // Note: this must be set after the schedule call
_readStreamRegistered = true; // Note: this must be set after the schedule call
CFReadStreamOpen(_readStream);
CFWriteStreamOpen(_writeStream);
}
else
{
if(op & SocketOperationWrite)
{
if(CFWriteStreamCanAcceptBytes(_writeStream))
{
readyOp = static_cast<SocketOperation>(readyOp | SocketOperationWrite);
}
else if(!_writeStreamRegistered)
{
CFWriteStreamScheduleWithRunLoop(_writeStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
_writeStreamRegistered = true; // Note: this must be set after the schedule call
if(CFWriteStreamCanAcceptBytes(_writeStream))
{
readyOp = static_cast<SocketOperation>(readyOp | SocketOperationWrite);
}
}
}
if(op & SocketOperationRead)
{
if(CFReadStreamHasBytesAvailable(_readStream))
{
readyOp = static_cast<SocketOperation>(readyOp | SocketOperationRead);
}
else if(!_readStreamRegistered)
{
CFReadStreamScheduleWithRunLoop(_readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
_readStreamRegistered = true; // Note: this must be set after the schedule call
if(CFReadStreamHasBytesAvailable(_readStream))
{
readyOp = static_cast<SocketOperation>(readyOp | SocketOperationRead);
}
}
}
}
return readyOp;
}
SocketOperation
IceObjC::StreamTransceiver::unregisterFromRunLoop(SocketOperation op, bool error)
{
IceUtil::Mutex::Lock sync(_mutex);
_error |= error;
if(_opening)
{
// Wait for the stream to be ready for write
if(op == SocketOperationWrite)
{
_writeStreamRegistered = false;
}
//
// We don't wait for the stream to be ready for read (even if
// it's a client connection) because there's no guarantees that
// the server might actually send data right away. If we use
// the WebSocket transport, the server actually waits for the
// client to write the HTTP upgrade request.
//
//if(op & SocketOperationRead && (_fd != INVALID_SOCKET || !(op & SocketOperationConnect)))
if(op == (SocketOperationRead | SocketOperationConnect))
{
_readStreamRegistered = false;
}
if(error || (!_readStreamRegistered && !_writeStreamRegistered))
{
CFWriteStreamUnscheduleFromRunLoop(_writeStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFReadStreamUnscheduleFromRunLoop(_readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
_opening = false;
return SocketOperationConnect;
}
else
{
return SocketOperationNone;
}
}
else
{
if(op & SocketOperationWrite && _writeStreamRegistered)
{
CFWriteStreamUnscheduleFromRunLoop(_writeStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
_writeStreamRegistered = false;
}
if(op & SocketOperationRead && _readStreamRegistered)
{
CFReadStreamUnscheduleFromRunLoop(_readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
_readStreamRegistered = false;
}
}
return op;
}
void
IceObjC::StreamTransceiver::closeStreams()
{
CFReadStreamSetClient(_readStream, kCFStreamEventNone, 0, 0);
CFWriteStreamSetClient(_writeStream, kCFStreamEventNone, 0, 0);
CFReadStreamClose(_readStream);
CFWriteStreamClose(_writeStream);
}
SocketOperation
IceObjC::StreamTransceiver::initialize(Buffer& readBuffer, Buffer& writeBuffer)
{
IceUtil::Mutex::Lock sync(_mutex);
if(_state == StateNeedConnect)
{
_state = StateConnectPending;
return SocketOperationConnect;
}
if(_state <= StateConnectPending)
{
if(_error)
{
CFErrorRef err = NULL;
if(CFWriteStreamGetStatus(_writeStream) == kCFStreamStatusError)
{
err = CFWriteStreamCopyError(_writeStream);
}
else if(CFReadStreamGetStatus(_readStream) == kCFStreamStatusError)
{
err = CFReadStreamCopyError(_readStream);
}
checkError(err, __FILE__, __LINE__);
}
_state = StateConnected;
if(_fd == INVALID_SOCKET)
{
if(!CFReadStreamSetProperty(_readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse) ||
!CFWriteStreamSetProperty(_writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse))
{
throw Ice::SocketException(__FILE__, __LINE__, 0);
}
CFDataRef d = (CFDataRef)CFReadStreamCopyProperty(_readStream, kCFStreamPropertySocketNativeHandle);
CFDataGetBytes(d, CFRangeMake(0, sizeof(SOCKET)), reinterpret_cast<UInt8*>(&_fd));
CFRelease(d);
}
ostringstream s;
Address localAddr;
fdToLocalAddress(_fd, localAddr);
s << "local address = " << addrToString(localAddr);
string proxyHost = _instance->proxyHost();
if(!proxyHost.empty())
{
s << "\nSOCKS proxy address = " << proxyHost << ":" << _instance->proxyPort();
}
Address remoteAddr;
bool peerConnected = fdToRemoteAddress(_fd, remoteAddr);
if(peerConnected)
{
s << "\nremote address = " << addrToString(remoteAddr);
}
else
{
s << "\nremote address = " << _host << ":" << _port;
}
_desc = s.str();
setBlock(_fd, false);
setTcpBufSize(_fd, _instance);
//
// Limit the size of packets passed to SSLWrite/SSLRead to avoid
// blocking and holding too much memory.
//
_maxSendPacketSize = std::max(512, getSendBufferSize(_fd));
_maxRecvPacketSize = std::max(512, getRecvBufferSize(_fd));
}
assert(_state == StateConnected);
return SocketOperationNone;
}
SocketOperation
#ifdef ICE_CPP11_MAPPING
IceObjC::StreamTransceiver::closing(bool initiator, exception_ptr)
#else
IceObjC::StreamTransceiver::closing(bool initiator, const Ice::LocalException&)
#endif
{
// If we are initiating the connection closure, wait for the peer
// to close the TCP/IP connection. Otherwise, close immediately.
return initiator ? SocketOperationRead : SocketOperationNone;
}
void
IceObjC::StreamTransceiver::close()
{
if(_fd != INVALID_SOCKET)
{
try
{
closeSocket(_fd);
_fd = INVALID_SOCKET;
}
catch(const SocketException&)
{
_fd = INVALID_SOCKET;
throw;
}
}
}
SocketOperation
IceObjC::StreamTransceiver::write(Buffer& buf)
{
IceUtil::Mutex::Lock sync(_mutex);
if(_error)
{
assert(CFWriteStreamGetStatus(_writeStream) == kCFStreamStatusError);
checkError(CFWriteStreamCopyError(_writeStream), __FILE__, __LINE__);
}
// Its impossible for the packetSize to be more than an Int.
size_t packetSize = std::min(static_cast<size_t>(buf.b.end() - buf.i), _maxSendPacketSize);
while(buf.i != buf.b.end())
{
if(!CFWriteStreamCanAcceptBytes(_writeStream))
{
return SocketOperationWrite;
}
if(_checkCertificates)
{
_checkCertificates = false;
checkCertificates();
}
assert(_fd != INVALID_SOCKET);
CFIndex ret = CFWriteStreamWrite(_writeStream, reinterpret_cast<const UInt8*>(&*buf.i), packetSize);
if(ret == SOCKET_ERROR)
{
if(CFWriteStreamGetStatus(_writeStream) == kCFStreamStatusAtEnd)
{
ConnectionLostException ex(__FILE__, __LINE__);
ex.error = getSocketErrno();
throw ex;
}
assert(CFWriteStreamGetStatus(_writeStream) == kCFStreamStatusError);
checkError(CFWriteStreamCopyError(_writeStream), __FILE__, __LINE__);
if(noBuffers() && packetSize > 1024)
{
packetSize /= 2;
}
continue;
}
buf.i += ret;
if(packetSize > buf.b.end() - buf.i)
{
packetSize = static_cast<int>(buf.b.end() - buf.i);
}
}
return SocketOperationNone;
}
SocketOperation
IceObjC::StreamTransceiver::read(Buffer& buf)
{
IceUtil::Mutex::Lock sync(_mutex);
if(_error)
{
assert(CFReadStreamGetStatus(_readStream) == kCFStreamStatusError);
checkError(CFReadStreamCopyError(_readStream), __FILE__, __LINE__);
}
// Its impossible for the packetSize to be more than an Int.
size_t packetSize = std::min(static_cast<size_t>(buf.b.end() - buf.i), _maxRecvPacketSize);
while(buf.i != buf.b.end())
{
if(!CFReadStreamHasBytesAvailable(_readStream))
{
return SocketOperationRead;
}
if(_checkCertificates)
{
_checkCertificates = false;
checkCertificates();
}
assert(_fd != INVALID_SOCKET);
CFIndex ret = CFReadStreamRead(_readStream, reinterpret_cast<UInt8*>(&*buf.i), packetSize);
if(ret == 0)
{
ConnectionLostException ex(__FILE__, __LINE__);
ex.error = 0;
throw ex;
}
if(ret == SOCKET_ERROR)
{
if(CFReadStreamGetStatus(_readStream) == kCFStreamStatusAtEnd)
{
ConnectionLostException ex(__FILE__, __LINE__);
ex.error = getSocketErrno();
throw ex;
}
assert(CFReadStreamGetStatus(_readStream) == kCFStreamStatusError);
checkError(CFReadStreamCopyError(_readStream), __FILE__, __LINE__);
if(noBuffers() && packetSize > 1024)
{
packetSize /= 2;
}
continue;
}
buf.i += ret;
if(packetSize > buf.b.end() - buf.i)
{
packetSize = static_cast<int>(buf.b.end() - buf.i);
}
}
return SocketOperationNone;
}
string
IceObjC::StreamTransceiver::protocol() const
{
return _instance->protocol();
}
string
IceObjC::StreamTransceiver::toString() const
{
return _desc;
}
string
IceObjC::StreamTransceiver::toDetailedString() const
{
return _desc;
}
Ice::ConnectionInfoPtr
IceObjC::StreamTransceiver::getInfo() const
{
if(_instance->secure())
{
IceSSL::ConnectionInfoPtr info = ICE_MAKE_SHARED(IceSSL::ConnectionInfo);
fillConnectionInfo(info);
info->verified = _state == StateConnected;
return info;
}
else
{
Ice::TCPConnectionInfoPtr info = ICE_MAKE_SHARED(Ice::TCPConnectionInfo);
fillConnectionInfo(info);
return info;
}
}
Ice::ConnectionInfoPtr
IceObjC::StreamTransceiver::getWSInfo(const Ice::HeaderDict& headers) const
{
if(_instance->secure())
{
IceSSL::WSSConnectionInfoPtr info = ICE_MAKE_SHARED(IceSSL::WSSConnectionInfo);
fillConnectionInfo(info);
info->verified = _state == StateConnected;
info->headers = headers;
return info;
}
else
{
Ice::WSConnectionInfoPtr info = ICE_MAKE_SHARED(Ice::WSConnectionInfo);
fillConnectionInfo(info);
info->headers = headers;
return info;
}
}
void
IceObjC::StreamTransceiver::checkSendSize(const Buffer& buf)
{
}
void
IceObjC::StreamTransceiver::setBufferSize(int rcvSize, int sndSize)
{
setTcpBufSize(_fd, rcvSize, sndSize, _instance);
}
IceObjC::StreamTransceiver::StreamTransceiver(const InstancePtr& instance,
CFReadStreamRef readStream,
CFWriteStreamRef writeStream,
const string& host,
Ice::Int port) :
StreamNativeInfo(INVALID_SOCKET),
_instance(instance),
_host(host),
_port(port),
_readStream(readStream),
_writeStream(writeStream),
_readStreamRegistered(false),
_writeStreamRegistered(false),
_opening(false),
_checkCertificates(instance->secure()),
_error(false),
_state(StateNeedConnect)
{
ostringstream s;
s << "local address = <not available>";
string proxyHost = instance->proxyHost();
if(!proxyHost.empty())
{
s << "\nSOCKS proxy address = " << proxyHost << ":" << instance->proxyPort();
}
s << "\nremote address = " << host << ":" << port;
_desc = s.str();
}
IceObjC::StreamTransceiver::StreamTransceiver(const InstancePtr& instance,
CFReadStreamRef readStream,
CFWriteStreamRef writeStream,
SOCKET fd) :
StreamNativeInfo(fd),
_instance(instance),
_port(0),
_readStream(readStream),
_writeStream(writeStream),
_readStreamRegistered(false),
_writeStreamRegistered(false),
_opening(false),
_checkCertificates(false),
_error(false),
_state(StateNeedConnect),
_desc(fdToString(fd))
{
}
IceObjC::StreamTransceiver::~StreamTransceiver()
{
assert(_fd == INVALID_SOCKET);
CFRelease(_readStream);
CFRelease(_writeStream);
}
void
IceObjC::StreamTransceiver::checkCertificates()
{
SecTrustRef trust = (SecTrustRef)CFWriteStreamCopyProperty(_writeStream, kCFStreamPropertySSLPeerTrust);
if(!trust)
{
throw Ice::SecurityException(__FILE__, __LINE__, "unable to obtain trust object");
}
try
{
SecPolicyRef policy = 0;
if(_host.empty() || _instance->properties()->getPropertyAsIntWithDefault("IceSSL.CheckCertName", 1) == 0)
{
policy = SecPolicyCreateBasicX509();
}
else
{
CFStringRef h = CFStringCreateWithCString(NULL, _host.c_str(), kCFStringEncodingUTF8);
policy = SecPolicyCreateSSL(false, h);
CFRelease(h);
}
OSStatus err = SecTrustSetPolicies(trust, policy);
CFRelease(policy);
if(err != noErr)
{
ostringstream os;
os << "unable to set trust object policy (error = " << err << ")";
throw Ice::SecurityException(__FILE__, __LINE__, os.str());
}
//
// If IceSSL.CertAuthFile is set, we use the certificate authorities from this file
// instead of the ones from the keychain.
//
if((err = SecTrustSetAnchorCertificates(trust, _instance->certificateAuthorities())) != noErr)
{
ostringstream os;
os << "couldn't set root CA certificates with trust object (error = " << err << ")";
throw Ice::SecurityException(__FILE__, __LINE__, os.str());
}
SecTrustResultType result = kSecTrustResultInvalid;
if((err = SecTrustEvaluate(trust, &result)) != noErr)
{
ostringstream os;
os << "unable to evaluate the peer certificate trust (error = " << err << ")";
throw Ice::SecurityException(__FILE__, __LINE__, os.str());
}
//
// The kSecTrustResultUnspecified result indicates that the user didn't set any trust
// settings for the root CA. This is expected if the root CA is provided by the user
// with IceSSL.CertAuthFile or if the user didn't explicitly set any trust settings
// for the certificate.
//
if(result != kSecTrustResultProceed && result != kSecTrustResultUnspecified)
{
ostringstream os;
os << "certificate validation failed (result = " << result << ")";
throw Ice::SecurityException(__FILE__, __LINE__, os.str());
}
if(_instance->trustOnlyKeyID())
{
if(SecTrustGetCertificateCount(trust) < 0)
{
throw Ice::SecurityException(__FILE__, __LINE__, "unable to obtain peer certificate");
}
SecCertificateRef cert = SecTrustGetCertificateAtIndex(trust, 0);
//
// To check the subject key ID, we add the peer certificate to the keychain with SetItemAdd,
// then we lookup for the cert using the kSecAttrSubjectKeyID. Then we remove the cert from
// the keychain. NOTE: according to the Apple documentation, it should in theory be possible
// to not add/remove the item to the keychain by specifying the kSecMatchItemList key (or
// kSecUseItemList?) when calling SecItemCopyMatching. Unfortunately this doesn't appear to
// work. Similarly, it should be possible to get back the attributes of the certificate
// once it added by setting kSecReturnAttributes in the add query, again this doesn't seem
// to work.
//
CFMutableDictionaryRef query;
query = CFDictionaryCreateMutable(0, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(query, kSecClass, kSecClassCertificate);
CFDictionarySetValue(query, kSecValueRef, cert);
err = SecItemAdd(query, 0);
if(err != noErr && err != errSecDuplicateItem)
{
CFRelease(query);
ostringstream os;
os << "unable to add peer certificate to keychain (error = " << err << ")";
throw Ice::SecurityException(__FILE__, __LINE__, os.str());
}
CFRelease(query);
query = CFDictionaryCreateMutable(0, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(query, kSecClass, kSecClassCertificate);
CFDictionarySetValue(query, kSecValueRef, cert);
CFDictionarySetValue(query, kSecAttrSubjectKeyID, _instance->trustOnlyKeyID());
err = SecItemCopyMatching(query, 0);
OSStatus foundErr = err;
CFRelease(query);
query = CFDictionaryCreateMutable(0, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(query, kSecClass, kSecClassCertificate);
CFDictionarySetValue(query, kSecValueRef, cert);
err = SecItemDelete(query);
if(err != noErr)
{
CFRelease(query);
ostringstream os;
os << "unable to remove peer certificate from keychain (error = " << err << ")";
throw Ice::SecurityException(__FILE__, __LINE__, os.str());
}
CFRelease(query);
if(foundErr != noErr)
{
ostringstream os;
os << "the certificate subject key ID doesn't match the `IceSSL.TrustOnly.Client' property ";
os << "(error = " << foundErr << ")";
throw Ice::SecurityException(__FILE__, __LINE__, os.str());
}
}
CFRelease(trust);
}
catch(...)
{
if(trust)
{
CFRelease(trust);
}
throw;
}
}
void
IceObjC::StreamTransceiver::checkError(CFErrorRef err, const char* file, int line)
{
assert(err);
CFStringRef domain = CFErrorGetDomain(err);
if(CFStringCompare(domain, kCFErrorDomainPOSIX, 0) == kCFCompareEqualTo)
{
errno = CFErrorGetCode(err);
CFRelease(err);
if(interrupted() || noBuffers())
{
return;
}
if(connectionLost())
{
ConnectionLostException ex(file, line);
ex.error = getSocketErrno();
throw ex;
}
else if(connectionRefused())
{
ConnectionRefusedException ex(file, line);
ex.error = getSocketErrno();
throw ex;
}
else if(connectFailed())
{
ConnectFailedException ex(file, line);
ex.error = getSocketErrno();
throw ex;
}
else
{
SocketException ex(file, line);
ex.error = getSocketErrno();
throw ex;
}
}
int error = CFErrorGetCode(err);
if(error == kCFHostErrorHostNotFound || error == kCFHostErrorUnknown)
{
int rs = 0;
if(error == kCFHostErrorUnknown)
{
CFDictionaryRef dict = CFErrorCopyUserInfo(err);
CFNumberRef d = (CFNumberRef)CFDictionaryGetValue(dict, kCFGetAddrInfoFailureKey);
if(d != 0)
{
CFNumberGetValue(d, kCFNumberSInt32Type, &rs);
}
CFRelease(dict);
}
CFRelease(err);
DNSException ex(file, line);
ex.error = rs;
ex.host = _host;
throw ex;
}
CFNetworkException ex(file, line);
ex.domain = fromCFString(domain);
ex.error = CFErrorGetCode(err);
CFRelease(err);
throw ex;
}
void
IceObjC::StreamTransceiver::fillConnectionInfo(const Ice::IPConnectionInfoPtr& info) const
{
fdToAddressAndPort(_fd, info->localAddress, info->localPort, info->remoteAddress, info->remotePort);
info->rcvSize = getRecvBufferSize(_fd);
info->sndSize = getSendBufferSize(_fd);
}
|