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
|
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
|
if (SSL_CTX_set_default_verify_paths(ctx) == 0)
@throw [OFInitializationFailedException
exceptionWithClass: self];
}
- initWithSocket: (OFTCPSocket*)socket
{
return [self initWithSocket: socket
privateKeyFile: nil
certificateFile: nil];
}
- initWithSocket: (OFTCPSocket*)socket
privateKeyFile: (OFString*)privateKeyFile
certificateFile: (OFString*)certificateFile
{
self = [self init];
@try {
/* FIXME: Also allow with accepted sockets */
_privateKeyFile = [privateKeyFile copy];
_certificateFile = [certificateFile copy];
_socket = dup(socket->_socket);
_socket = dup(socket->_socket);
if ((_SSL = SSL_new(ctx)) == NULL ||
!SSL_set_fd(_SSL, _socket)) {
close(_socket);
_socket = INVALID_SOCKET;
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
}
SSL_set_connect_state(_SSL);
if ((_privateKeyFile != nil && !SSL_use_PrivateKey_file(_SSL,
[_privateKeyFile cStringWithEncoding:
OF_STRING_ENCODING_NATIVE], SSL_FILETYPE_PEM)) ||
(_certificateFile != nil && !SSL_use_certificate_file(_SSL,
[_certificateFile cStringWithEncoding:
OF_STRING_ENCODING_NATIVE], SSL_FILETYPE_PEM)) ||
SSL_connect(_SSL) != 1) {
close(_socket);
_socket = INVALID_SOCKET;
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
}
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
SSL *SSL_ = _SSL;
[_privateKeyFile release];
[_certificateFile release];
[super dealloc];
if (SSL_ != NULL)
SSL_free(SSL_);
}
- (void)connectToHost: (OFString*)host
- (void)startTLS
port: (uint16_t)port
{
[super connectToHost: host
port: port];
if ((_SSL = SSL_new(ctx)) == NULL || !SSL_set_fd(_SSL, _socket)) {
[super close];
@throw [OFConnectionFailedException
exceptionWithClass: [self class]
socket: self
host: host
port: port];
host: nil
port: 0];
}
SSL_set_connect_state(_SSL);
if ((_privateKeyFile != nil && !SSL_use_PrivateKey_file(_SSL,
[_privateKeyFile cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
SSL_FILETYPE_PEM)) || (_certificateFile != nil &&
!SSL_use_certificate_file(_SSL, [_certificateFile
cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
SSL_FILETYPE_PEM)) || SSL_connect(_SSL) != 1) {
[super close];
@throw [OFConnectionFailedException
exceptionWithClass: [self class]
socket: self
host: nil
port: 0];
}
}
- (void)connectToHost: (OFString*)host
port: (uint16_t)port
{
[super connectToHost: host
port: port];
@try {
[self startTLS];
} @catch (OFConnectionFailedException *e) {
@throw [OFConnectionFailedException
exceptionWithClass: [self class]
socket: self
host: host
port: port];
}
}
- (SSLSocket*)accept
- (instancetype)accept
{
SSLSocket *client = (SSLSocket*)[super accept];
if ((client->_SSL = SSL_new(ctx)) == NULL ||
!SSL_set_fd(client->_SSL, client->_socket)) {
[client SSL_super_close];
@throw [OFAcceptFailedException exceptionWithClass: [self class]
|
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
|
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
|
-
+
-
+
+
+
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
{
if (_SSL == NULL)
return [super numberOfBytesInReadBuffer];
return [super numberOfBytesInReadBuffer] + SSL_pending(_SSL);
}
- (void)setPrivateKeyFile: (OFString*)privateKeyFile
- (void)setDelegate: (id <OFTLSSocketDelegate>)delegate
{
OF_SETTER(_privateKeyFile, privateKeyFile, true, 1)
/* FIXME */
[self doesNotRecognizeSelector: _cmd];
abort();
}
- (OFString*)privateKeyFile
- (id <OFTLSSocketDelegate>)delegate
{
/* FIXME */
OF_GETTER(_privateKeyFile, true)
return nil;
}
- (void)setCertificateFile: (OFString*)certificateFile
{
OF_SETTER(_certificateFile, certificateFile, true, 1)
}
- (OFString*)certificateFile
{
OF_GETTER(_certificateFile, true)
}
- (void)setPrivateKeyFile: (OFString*)privateKeyFile
{
OF_SETTER(_privateKeyFile, privateKeyFile, true, 1)
}
- (OFString*)privateKeyFile
{
OF_GETTER(_privateKeyFile, true)
}
- (void)setPrivateKeyPassphrase: (const char*)privateKeyPassphrase
{
/* FIXME */
}
- (const char*)privateKeyPassphrase
{
/* FIXME */
return NULL;
}
- (void)setRequestsClientCertificates: (bool)enabled
{
_requestsClientCertificates = enabled;
}
- (bool)requestsClientCertificates
|