ObjOpenSSL  Diff

Differences From Artifact [913170a06a]:

To Artifact [65a015edb7]:


1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2011, 2012, 2013, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2011, 2012, 2013, 2014, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 * Copyright (c) 2011, Jos Kuijpers <jos@kuijpersvof.nl>
 *
 * https://webkeks.org/git/?p=objopenssl.git
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
133
134
135
136
137
138
139
140


141
142
143
144
145
146
147
148
149


150
151
152
153
154
155
156
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







-
+
+









+
+








	if (SSL_ != NULL)
		SSL_free(SSL_);
}

- (void)startTLS
{
	of_string_encoding_t encoding = [OFString nativeOSEncoding];
	of_string_encoding_t encoding;

	if ((_SSL = SSL_new(ctx)) == NULL || !SSL_set_fd(_SSL, _socket)) {
		[super close];
		@throw [OFConnectionFailedException
		    exceptionWithHost: nil
				 port: 0
			       socket: self];
	}

	SSL_set_connect_state(_SSL);

	encoding = [OFString nativeOSEncoding];

	if ((_privateKeyFile != nil && !SSL_use_PrivateKey_file(_SSL,
	    [_privateKeyFile cStringWithEncoding: encoding],
	    SSL_FILETYPE_PEM)) || (_certificateFile != nil &&
	    !SSL_use_certificate_file(_SSL, [_certificateFile
	    cStringWithEncoding: encoding],
	    SSL_FILETYPE_PEM)) || SSL_connect(_SSL) != 1) {
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
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







-

+












+







			port: port];

	[self startTLS];
}

- (instancetype)accept
{
	of_string_encoding_t encoding = [OFString nativeOSEncoding];
	SSLSocket *client = (SSLSocket*)[super accept];
	of_string_encoding_t encoding;

	if ((client->_SSL = SSL_new(ctx)) == NULL ||
	    !SSL_set_fd(client->_SSL, client->_socket)) {
		[client SSL_super_close];
		@throw [OFAcceptFailedException exceptionWithSocket: self];
	}

	if (_requestsClientCertificates)
		SSL_set_verify(client->_SSL, SSL_VERIFY_PEER, NULL);

	SSL_set_accept_state(client->_SSL);

	encoding = [OFString nativeOSEncoding];
	if (!SSL_use_PrivateKey_file(client->_SSL, [_privateKeyFile
	    cStringWithEncoding: encoding],
	    SSL_FILETYPE_PEM) || !SSL_use_certificate_file(client->_SSL,
	    [_certificateFile cStringWithEncoding: encoding],
	    SSL_FILETYPE_PEM) || SSL_accept(client->_SSL) != 1) {
		[client SSL_super_close];
		@throw [OFAcceptFailedException exceptionWithSocket: self];
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
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







-
+











-
+


-
+








	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];

	if (_atEndOfStream) {
		OFReadFailedException *e;

		e = [OFReadFailedException exceptionWithStream: self
		e = [OFReadFailedException exceptionWithObject: self
					       requestedLength: length];
#ifndef _WIN32
		e->_errNo = ENOTCONN;
#else
		e->_errNo = WSAENOTCONN;
#endif

		@throw e;
	}

	if ((ret = SSL_read(_SSL, buffer, (int)length)) < 0) {
		if (SSL_get_error(_SSL, ret) ==  SSL_ERROR_WANT_READ)
		if (SSL_get_error(_SSL, ret) == SSL_ERROR_WANT_READ)
			return 0;

		@throw [OFReadFailedException exceptionWithStream: self
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];
	}

	if (ret == 0)
		_atEndOfStream = true;

	return ret;
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
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







-
+












-
+



-
+

-
-
+
+

-
+








	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];

	if (_atEndOfStream) {
		OFWriteFailedException *e;

		e = [OFWriteFailedException exceptionWithStream: self
		e = [OFWriteFailedException exceptionWithObject: self
						requestedLength: length];

#ifndef _WIN32
		e->_errNo = ENOTCONN;
#else
		e->_errNo = WSAENOTCONN;
#endif

		@throw e;
	}

	if (SSL_write(_SSL, buffer, (int)length) < length)
		@throw [OFWriteFailedException exceptionWithStream: self
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
}

- (size_t)numberOfBytesInReadBuffer
- (bool)hasDataInReadBuffer
{
	if (_SSL == NULL)
		return [super numberOfBytesInReadBuffer];
	if (_SSL != NULL && SSL_pending(_SSL) > 0)
		return true;

	return [super numberOfBytesInReadBuffer] + SSL_pending(_SSL);
	return [super hasDataInReadBuffer];
}

- (void)setDelegate: (id <OFTLSSocketDelegate>)delegate
{
	/* FIXME */
	[self doesNotRecognizeSelector: _cmd];
	abort();