ObjOpenSSL  Check-in [071d74e840]

Overview
Comment:Conform to OFTLSSocket

This also introduces automatic certificate verification and enables it
by default, as required by OFTLSSocket.

There are still TODOs until OFTLSSocket is fully implemented.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 071d74e84035d0cf764ba6089ccc004f6d617239c8f78eb8fe86021d3c0286be
User & Date: js on 2015-05-24 19:33:09
Other Links: manifest | tags
Context
2016-03-06
17:48
Adjust to ObjFW changes check-in: aab1410a4b user: js tags: trunk
2015-05-24
19:33
Conform to OFTLSSocket check-in: 071d74e840 user: js tags: trunk
2015-05-07
21:33
Update config.guess, config.sub and install-sh check-in: 3dfd66b64f user: js tags: trunk
Changes

Modified configure.ac from [66416e9578] to [03f212863f].

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
AC_ARG_ENABLE(static, AS_HELP_STRING([--enable-static], [build static library]))
AS_IF([test x"$enable_static" = x"yes" -o x"$enable_shared" = x"no"], [
	AC_PATH_TOOL(AR, ar)
	AC_PROG_RANLIB
	AC_SUBST(OBJOPENSSL_STATIC_LIB, "libobjopenssl.a")
])

AC_CHECK_LIB(ssl, main, [LIBS="$LIBS -lssl"], [
	AC_MSG_ERROR(No libssl found! Please install OpenSSL!)
])
AC_CHECK_LIB(crypto, main, [LIBS="$LIBS -lcrypto"], [
	AC_MSG_ERROR(No libcrypto found! Please install OpenSSL!)
])
AC_CHECK_LIB(z, main, [LIBS="$LIBS -lz"], [
	AC_MSG_ERROR(No libz found! Please install zlib!)
])

AS_IF([test x"$GOBJC" = x"yes"], [
	OBJCFLAGS="$OBJCFLAGS -Wwrite-strings -Wpointer-arith"
	dnl We need -Wno-deprecated-declarations as OpenSSL is deprecated on
	dnl OS X.
	OBJCFLAGS="$OBJCFLAGS -Wno-deprecated-declarations"
])

BUILDSYS_INIT

dnl We don't call AC_PROG_CPP, but only AC_PROG_OBJCPP and set CPP to OBJCPP
dnl and add OBJCPPFLAGS to CPPFLAGS, thus we need to AC_SUBST these ourself.
AC_SUBST(CPP)
AC_SUBST(CPPFLAGS)

AC_SUBST(PACKAGE, ObjOpenSSL)
AC_CONFIG_FILES([buildsys.mk extra.mk])
AC_OUTPUT







|
|
<
|
<
|
<
|



|
<
<
<












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
AC_ARG_ENABLE(static, AS_HELP_STRING([--enable-static], [build static library]))
AS_IF([test x"$enable_static" = x"yes" -o x"$enable_shared" = x"no"], [
	AC_PATH_TOOL(AR, ar)
	AC_PROG_RANLIB
	AC_SUBST(OBJOPENSSL_STATIC_LIB, "libobjopenssl.a")
])

PKG_CHECK_MODULES(openssl, [openssl >= 1.0.2], [
	CPPFLAGS="$CPPFLAGS $openssl_CFLAGS"

	LIBS="$LIBS $openssl_LIBS"

], [

	AC_MSG_ERROR(You need openssl >= 1.0.2 installed!)
])

AS_IF([test x"$GOBJC" = x"yes"], [
	OBJCFLAGS="$OBJCFLAGS -Wwrite-strings -Wpointer-arith -Werror"



])

BUILDSYS_INIT

dnl We don't call AC_PROG_CPP, but only AC_PROG_OBJCPP and set CPP to OBJCPP
dnl and add OBJCPPFLAGS to CPPFLAGS, thus we need to AC_SUBST these ourself.
AC_SUBST(CPP)
AC_SUBST(CPPFLAGS)

AC_SUBST(PACKAGE, ObjOpenSSL)
AC_CONFIG_FILES([buildsys.mk extra.mk])
AC_OUTPUT

Modified src/SSLSocket.h from [127e317cb2] to [536accb4df].

29
30
31
32
33
34
35

36
37
38
39

40
41
42
43
44
45
46
47
48
49
50
@class X509Certificate;

@interface SSLSocket: OFTCPSocket <OFTLSSocket>
{
	SSL *_SSL;
	OFString *_certificateFile, *_privateKeyFile;
	const char *_privateKeyPassphrase;

	bool _requestsClientCertificates;
}

#ifdef OF_HAVE_PROPERTIES

@property bool requestsClientCertificates;
#endif

- initWithSocket: (OFTCPSocket*)socket;
- (void)SSL_super_close;
- (void)setRequestsClientCertificates: (bool)enabled;
- (bool)requestsClientCertificates;
- (OFDataArray*)channelBindingDataWithType: (OFString*)type;
- (X509Certificate*)peerCertificate;
- (void)verifyPeerCertificate;
@end







>
|



>
|




|
|




29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@class X509Certificate;

@interface SSLSocket: OFTCPSocket <OFTLSSocket>
{
	SSL *_SSL;
	OFString *_certificateFile, *_privateKeyFile;
	const char *_privateKeyPassphrase;
	bool _certificateVerificationEnabled;
	bool _requestClientCertificatesEnabled;
}

#ifdef OF_HAVE_PROPERTIES
@property (getter=isRequestClientCertificatesEnabled)
    bool requestClientCertificatesEnabled;
#endif

- initWithSocket: (OFTCPSocket*)socket;
- (void)SSL_super_close;
- (void)setRequestClientCertificatesEnabled: (bool)enabled;
- (bool)isRequestClientCertificatesEnabled;
- (OFDataArray*)channelBindingDataWithType: (OFString*)type;
- (X509Certificate*)peerCertificate;
- (void)verifyPeerCertificate;
@end

Modified src/SSLSocket.m from [0f120f580c] to [cd16825332].

22
23
24
25
26
27
28





29
30
31





32
33
34
35
36
37
38
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <unistd.h>
#include <errno.h>
#include <assert.h>






#include <openssl/crypto.h>
#include <openssl/err.h>






#import <ObjFW/OFThread.h>
#import <ObjFW/OFHTTPRequest.h>
#import <ObjFW/OFDataArray.h>
#import <ObjFW/OFSystemInfo.h>

#import <ObjFW/OFAcceptFailedException.h>







>
>
>
>
>



>
>
>
>
>







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
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <unistd.h>
#include <errno.h>
#include <assert.h>

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdocumentation"
#endif

#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/x509v3.h>

#if defined(__clang__)
# pragma clang diagnostic pop
#endif

#import <ObjFW/OFThread.h>
#import <ObjFW/OFHTTPRequest.h>
#import <ObjFW/OFDataArray.h>
#import <ObjFW/OFSystemInfo.h>

#import <ObjFW/OFAcceptFailedException.h>
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
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];

	if (SSL_CTX_set_default_verify_paths(ctx) == 0)
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
}










- initWithSocket: (OFTCPSocket*)socket
{
	self = [self init];


	_socket = dup(socket->_socket);






	return self;
}

- (void)dealloc
{
	SSL *SSL_ = _SSL;

	[_privateKeyFile release];
	[_certificateFile release];

	[super dealloc];

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

- (void)startTLS

{
	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 = [OFSystemInfo native8BitEncoding];

	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) {
		[super close];
		@throw [OFConnectionFailedException
		    exceptionWithHost: nil
				 port: 0
			       socket: self];
	}
}







- (void)connectToHost: (OFString*)host
		 port: (uint16_t)port
{
	[super connectToHost: host
			port: port];

	[self startTLS];

}

- (instancetype)accept
{
	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];
		/* FIXME: Get a proper errno */
		@throw [OFAcceptFailedException exceptionWithSocket: self
							      errNo: 0];
	}

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

	SSL_set_accept_state(client->_SSL);

	encoding = [OFSystemInfo native8BitEncoding];

	if (!SSL_use_PrivateKey_file(client->_SSL, [_privateKeyFile







>
>
>
>
>
>
>
>
>





>
|
>
>
>
>
>

















|
>






|
|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













|
|



>
>
>
>
>
>







|
>















|







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
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];

	if (SSL_CTX_set_default_verify_paths(ctx) == 0)
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
}

- init
{
	self = [super init];

	_certificateVerificationEnabled = true;

	return self;
}

- initWithSocket: (OFTCPSocket*)socket
{
	self = [self init];

	@try {
		if ((_socket = dup(socket->_socket)) < 0)
			@throw [OFInitializationFailedException exception];
	} @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)SSL_startTLSWithExpectedHost: (OFString*)host
				port: (uint16_t)port
{
	of_string_encoding_t encoding;

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

	if (_certificateVerificationEnabled) {
		X509_VERIFY_PARAM *param = SSL_get0_param(_SSL);

		X509_VERIFY_PARAM_set_hostflags(param,
		    X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);

		if (X509_VERIFY_PARAM_set1_host(param,
		    [host UTF8String], [host UTF8StringLength]) == 0)
			@throw [OFConnectionFailedException
			    exceptionWithHost: host
					 port: port
				       socket: self];

		SSL_set_verify(_SSL, SSL_VERIFY_PEER, NULL);
	}

	SSL_set_connect_state(_SSL);

	encoding = [OFSystemInfo native8BitEncoding];

	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) {
		[super close];
		@throw [OFConnectionFailedException
		    exceptionWithHost: host
				 port: port
			       socket: self];
	}
}

- (void)startTLSWithExpectedHost: (OFString*)host
{
	[self SSL_startTLSWithExpectedHost: host
				      port: 0];
}

- (void)connectToHost: (OFString*)host
		 port: (uint16_t)port
{
	[super connectToHost: host
			port: port];

	[self SSL_startTLSWithExpectedHost: host
				      port: port];
}

- (instancetype)accept
{
	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];
		/* FIXME: Get a proper errno */
		@throw [OFAcceptFailedException exceptionWithSocket: self
							      errNo: 0];
	}

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

	SSL_set_accept_state(client->_SSL);

	encoding = [OFSystemInfo native8BitEncoding];

	if (!SSL_use_PrivateKey_file(client->_SSL, [_privateKeyFile
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
		return true;

	return [super hasDataInReadBuffer];
}

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

- (id <OFTLSSocketDelegate>)delegate
{
	/* FIXME */
	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
{
	return _requestsClientCertificates;
}

- (OFDataArray*)channelBindingDataWithType: (OFString*)type
{
	size_t length;
	char buffer[64];
	OFDataArray *data;







|
|
<




|
<
>






>
>
>
>
>
>
>





>
>
>
>
>
>





>
>
>
>
>
>
>






>
>
>
>
>
>


>
>
>
|
>
>
>
>
>




>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|


|

|


|

|







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

	return [super hasDataInReadBuffer];
}

- (void)setDelegate: (id <OFTLSSocketDelegate>)delegate
{
	/* TODO */
	OF_UNRECOGNIZED_SELECTOR

}

- (id <OFTLSSocketDelegate>)delegate
{
	/* TODO */

	OF_UNRECOGNIZED_SELECTOR
}

- (void)setCertificateFile: (OFString*)certificateFile
{
	OF_SETTER(_certificateFile, certificateFile, true, 1)
}

- (void)setCertificateFile: (OFString*)certificateFile
		forSNIHost: (OFString*)SNIHost
{
	/* TODO */
	OF_UNRECOGNIZED_SELECTOR
}

- (OFString*)certificateFile
{
	OF_GETTER(_certificateFile, true)
}

- (OFString*)certificateFileForSNIHost: (OFString*)SNIHost
{
	/* TODO */
	OF_UNRECOGNIZED_SELECTOR
}

- (void)setPrivateKeyFile: (OFString*)privateKeyFile
{
	OF_SETTER(_privateKeyFile, privateKeyFile, true, 1)
}

- (void)setPrivateKeyFile: (OFString*)privateKeyFile
	       forSNIHost: (OFString*)SNIHost
{
	/* TODO */
	OF_UNRECOGNIZED_SELECTOR
}

- (OFString*)privateKeyFile
{
	OF_GETTER(_privateKeyFile, true)
}

- (OFString*)privateKeyFileForSNIHost: (OFString*)SNIHost
{
	/* TODO */
	OF_UNRECOGNIZED_SELECTOR
}

- (void)setPrivateKeyPassphrase: (const char*)privateKeyPassphrase
{
	/* TODO */
	OF_UNRECOGNIZED_SELECTOR
}

- (void)setPrivateKeyPassphrase: (const char*)privateKeyPassphrase
		     forSNIHost: (OFString*)SNIHost
{
	/* TODO */
	OF_UNRECOGNIZED_SELECTOR
}

- (const char*)privateKeyPassphrase
{
	/* TODO */
	OF_UNRECOGNIZED_SELECTOR
}

- (const char*)privateKeyPassphraseForSNIHost: (OFString*)SNIHost
{
	/* TODO */
	OF_UNRECOGNIZED_SELECTOR
}

- (void)setCertificateVerificationEnabled: (bool)enabled
{
	_certificateVerificationEnabled = enabled;
}

- (bool)isCertificateVerificationEnabled
{
	return _certificateVerificationEnabled;
}

- (void)setRequestClientCertificatesEnabled: (bool)enabled
{
	_requestClientCertificatesEnabled = enabled;
}

- (bool)isRequestClientCertificatesEnabled
{
	return _requestClientCertificatesEnabled;
}

- (OFDataArray*)channelBindingDataWithType: (OFString*)type
{
	size_t length;
	char buffer[64];
	OFDataArray *data;

Modified src/X509Certificate.m from [28ac2990f0] to [2e798742d9].

16
17
18
19
20
21
22





23
24
25




26
27
28
29
30
31
32
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */






#include <openssl/crypto.h>
#include <openssl/x509v3.h>





#import "X509Certificate.h"

#import <ObjFW/OFAutoreleasePool.h>
#import <ObjFW/OFArray.h>
#import <ObjFW/OFDataArray.h>
#import <ObjFW/OFDictionary.h>







>
>
>
>
>



>
>
>
>







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
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdocumentation"
#endif

#include <openssl/crypto.h>
#include <openssl/x509v3.h>

#if defined(__clang__)
# pragma clang diagnostic pop
#endif

#import "X509Certificate.h"

#import <ObjFW/OFAutoreleasePool.h>
#import <ObjFW/OFArray.h>
#import <ObjFW/OFDataArray.h>
#import <ObjFW/OFDictionary.h>