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
|
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
|
-
-
+
+
-
-
-
-
+
+
+
-
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
+
+
-
+
+
+
+
+
-
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#import "OFTLSSocket.h"
#import "OFSocket.h"
#import "OFTLSStream.h"
#import "OFDate.h"
#import "OFSocket+Private.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
Class OFTLSSocketImplementation = Nil;
Class OFTLSStreamImplementation = Nil;
static const OFRunLoopMode handshakeRunLoopMode =
@"OFTLSStreamHandshakeRunLoopMode";
@interface OFTLSSocketAsyncConnector: OFObject <OFTLSSocketDelegate>
@interface OFTLSStreamHandshakeDelegate: OFObject <OFTLSStreamDelegate>
{
@public
OFTLSSocket *_socket;
OFString *_host;
uint16_t _port;
id <OFTLSSocketDelegate> _delegate;
}
- (instancetype)initWithSocket: (OFTLSSocket *)sock
host: (OFString *)host
bool _done;
id _exception;
}
@end
@implementation OFTLSStreamHandshakeDelegate
- (void)dealloc
{
[_exception release];
[super dealloc];
}
- (void)stream: (OFTLSStream *)stream
didPerformClientHandshakeWithHost: (OFString *)host
port: (uint16_t)port
delegate: (id <OFTLSSocketDelegate>)delegate;
exception: (id)exception
{
_done = true;
_exception = [exception retain];
}
@end
@implementation OFTLSSocketAsyncConnector
- (instancetype)initWithSocket: (OFTLSSocket *)sock
host: (OFString *)host
port: (uint16_t)port
delegate: (id <OFTLSSocketDelegate>)delegate
@implementation OFTLSStream
@synthesize wrappedStream = _wrappedStream;
@dynamic delegate;
@synthesize verifiesCertificates = _verifiesCertificates;
+ (instancetype)alloc
{
if (self == [OFTLSStream class]) {
if (OFTLSStreamImplementation != Nil)
return [OFTLSStreamImplementation alloc];
@throw [OFNotImplementedException exceptionWithSelector: _cmd
object: self];
}
return [super alloc];
}
+ (instancetype)streamWithStream: (OFStream <OFReadyForReadingObserving,
OFReadyForWritingObserving> *)stream
{
return [[[self alloc] initWithStream: stream] autorelease];
}
- (instancetype)init
{
OF_INVALID_INIT_METHOD
}
- (instancetype)initWithStream: (OFStream <OFReadyForReadingObserving,
OFReadyForWritingObserving> *)stream
{
self = [super init];
@try {
_socket = [sock retain];
_wrappedStream = [stream retain];
_host = [host copy];
_port = port;
_delegate = [delegate retain];
_verifiesCertificates = true;
_socket.delegate = self;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
if (_socket.delegate == self)
_socket.delegate = _delegate;
[_socket release];
[_wrappedStream release];
[_delegate release];
[super dealloc];
}
- (void)socket: (OFTCPSocket *)sock
didConnectToHost: (OFString *)host
port: (uint16_t)port
exception: (id)exception
{
if (exception == nil) {
@try {
[(OFTLSSocket *)sock startTLSForHost: _host
port: _port];
} @catch (id e) {
[self release];
@throw e;
}
}
_socket.delegate = _delegate;
[_delegate socket: sock
didConnectToHost: host
port: port
exception: exception];
}
@end
@implementation OFTLSSocket
@dynamic delegate;
@synthesize verifiesCertificates = _verifiesCertificates;
+ (instancetype)alloc
{
if (self == [OFTLSSocket class]) {
if (OFTLSSocketImplementation == nil)
@throw [OFNotImplementedException
exceptionWithSelector: _cmd
object: self];
return [OFTLSSocketImplementation alloc];
}
return [super alloc];
}
- (instancetype)init
{
self = [super init];
_verifiesCertificates = true;
return self;
}
- (instancetype)initWithSocket: (OFTCPSocket *)socket
{
self = [super init];
@try {
if ([socket isKindOfClass: [OFTLSSocket class]])
@throw [OFInvalidArgumentException exception];
_socket = socket->_socket;
socket->_socket = OFInvalidSocketHandle;
_verifiesCertificates = true;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)startTLSForHost: (OFString *)host port: (uint16_t)port
{
OF_UNRECOGNIZED_SELECTOR
}
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
runLoopMode: (OFRunLoopMode)runLoopMode
{
void *pool = objc_autoreleasePoolPush();
[[[OFTLSSocketAsyncConnector alloc]
initWithSocket: self
host: host
port: port
delegate: _delegate] autorelease];
[super asyncConnectToHost: host port: port runLoopMode: runLoopMode];
objc_autoreleasePoolPop(pool);
}
#ifdef OF_HAVE_BLOCKS
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
runLoopMode: (OFRunLoopMode)runLoopMode
block: (OFTCPSocketAsyncConnectBlock)block
{
[super asyncConnectToHost: host
port: port
runLoopMode: runLoopMode
block: ^ (id exception) {
if (exception == nil) {
@try {
[self startTLSForHost: host port: port];
} @catch (id e) {
block(e);
return;
}
}
block(exception);
}];
}
#endif
- (instancetype)accept
{
OF_UNRECOGNIZED_SELECTOR
}
- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length
{
OF_UNRECOGNIZED_SELECTOR
}
- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length
{
OF_UNRECOGNIZED_SELECTOR
}
- (bool)hasDataInReadBuffer
{
return (super.hasDataInReadBuffer ||
_wrappedStream.hasDataInReadBuffer);
}
- (instancetype)TCPAccept
- (bool)lowlevelIsAtEndOfStream
{
return [super accept];
return _wrappedStream.atEndOfStream;
}
- (int)fileDescriptorForReading
{
return _wrappedStream.fileDescriptorForReading;
}
- (size_t)lowlevelTCPReadIntoBuffer: (void *)buffer length: (size_t)length
- (int)fileDescriptorForWriting
{
return _wrappedStream.fileDescriptorForWriting;
}
return [super lowlevelReadIntoBuffer: buffer length: length];
- (void)asyncPerformClientHandshakeWithHost: (OFString *)host
{
[self asyncPerformClientHandshakeWithHost: host
runLoopMode: OFDefaultRunLoopMode];
}
- (void)asyncPerformClientHandshakeWithHost: (OFString *)host
runLoopMode: (OFRunLoopMode)runLoopMode
{
OF_UNRECOGNIZED_SELECTOR
}
- (size_t)lowlevelTCPWriteBuffer: (const void *)buffer length: (size_t)length
- (void)performClientHandshakeWithHost: (OFString *)host
{
void *pool = objc_autoreleasePoolPush();
id <OFTLSStreamDelegate> delegate = _delegate;
OFTLSStreamHandshakeDelegate *handshakeDelegate =
[[[OFTLSStreamHandshakeDelegate alloc] init] autorelease];
OFRunLoop *runLoop = [OFRunLoop currentRunLoop];
return [super lowlevelWriteBuffer: buffer length: length];
}
_delegate = handshakeDelegate;
[self asyncPerformClientHandshakeWithHost: host
runLoopMode: handshakeRunLoopMode];
while (!handshakeDelegate->_done)
[runLoop runMode: handshakeRunLoopMode beforeDate: nil];
/* Cleanup */
[runLoop runMode: handshakeRunLoopMode beforeDate: [OFDate date]];
- (bool)lowlevelTCPIsAtEndOfStream
{
return [super lowlevelIsAtEndOfStream];
_delegate = delegate;
if (handshakeDelegate->_exception != nil)
@throw handshakeDelegate->_exception;
objc_autoreleasePoolPop(pool);
}
@end
|