ObjOpenSSL  Diff

Differences From Artifact [756adaa84b]:

To Artifact [f2bb7cedd4]:


1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016
 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
 *     Jonathan Schleifer <js@heap.zone>
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 * Copyright (c) 2011, Jos Kuijpers <jos@kuijpersvof.nl>
 *
 * https://heap.zone/git/objopenssl.git
 *
 * Permission to use, copy, modify, and/or distribute this software for any
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
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







-
+

+


-
+
-
-


-
-
-
+
+
+
+
-
-
-
-
-


-
-
-
-
+
+
+
+
+
-
-




+


-
-
+
+
-
+










+
+
-
-
-
+
+
+




-
-
-
+
+
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




+
-
+








@interface SSLSocket ()
- (void)SSL_startTLSWithExpectedHost: (OFString *)host
				port: (uint16_t)port;
- (void)SSL_super_close;
@end

@interface SSLSocket_ConnectContext: OFObject
@interface SSLSocket_ConnectDelegate: OFObject <OFTLSSocketDelegate>
{
	SSLSocket *_socket;
	OFString *_host;
	uint16_t _port;
	id _target;
	id <OFTLSSocketDelegate> _delegate;
	SEL _selector;
	id _context;
}

- (instancetype)initWithHost: (OFString *)host
			port: (uint16_t)port
		      target: (id)target
- (instancetype)initWithSocket: (SSLSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		      delegate: (id <OFTLSSocketDelegate>)delegate;
		    selector: (SEL)selector
		     context: (id)context;
- (void)socketDidConnect: (SSLSocket *)sock
		 context: (id)context
	       exception: (id)exception;
@end

@implementation SSLSocket_ConnectContext
- (instancetype)initWithHost: (OFString *)host
			port: (uint16_t)port
		      target: (id)target
@implementation SSLSocket_ConnectDelegate
- (instancetype)initWithSocket: (SSLSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		      delegate: (id <OFTLSSocketDelegate>)delegate
		    selector: (SEL)selector
		     context: (id)context
{
	self = [super init];

	@try {
		_socket = [sock retain];
		_host = [host copy];
		_port = port;
		_target = [target retain];
		_selector = selector;
		_delegate = [delegate retain];

		_context = [context retain];
		[_socket setDelegate: self];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	if ([_socket delegate] == self)
		[_socket setDelegate: _delegate];
	[_host release];
	[_target release];
	[_context release];

	[_socket release];
	[_delegate release];

	[super dealloc];
}

- (void)socketDidConnect: (SSLSocket *)sock
		 context: (id)context
	       exception: (id)exception
-     (void)socket: (OF_KINDOF(OFTCPSocket *))sock
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
{
	void (*func)(id, SEL, OFTCPSocket *, id, id) =
	    (void (*)(id, SEL, OFTCPSocket *, id, id))
	    [_target methodForSelector: _selector];

	if (exception == nil) {
		@try {
			[sock SSL_startTLSWithExpectedHost: _host
						      port: _port];
		} @catch (id e) {
			func(_target, _selector, sock, _context, e);
			return;
		}
	}

	func(_target, _selector, sock, _context, exception);
	@try {
		[sock SSL_startTLSWithExpectedHost: _host
					      port: _port];
	} @catch (id e) {
		[_socket setDelegate: _delegate];
		[_delegate		   socket: sock
		    didFailToConnectWithException: e
					     host: host
					     port: port];
		return;
	}

	[_socket setDelegate: _delegate];
	[_delegate    socket: sock
	    didConnectToHost: host
			port: port];
}

-		   (void)socket: (OF_KINDOF(OFTCPSocket *))sock
  didFailToConnectWithException: (id)exception
			   host: (OFString *)host
			   port: (uint16_t)port
{
	[_socket setDelegate: _delegate];

	return [_delegate	   socket: sock
	    didFailToConnectWithException: exception
				     host: host
				     port: port];
}
@end

@implementation SSLSocket
@dynamic delegate;
@synthesize delegate = _delegate, certificateFile = _certificateFile;
@synthesize certificateFile = _certificateFile;
@synthesize privateKeyFile = _privateKeyFile;
@synthesize privateKeyPassphrase = _privateKeyPassphrase;
@synthesize certificateVerificationEnabled = _certificateVerificationEnabled;
@synthesize requestClientCertificatesEnabled =
    _requestClientCertificatesEnabled;

+ (void)load
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
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







-
-
-


-
+

-
-
-
+
+
+
+
-
-
-
+


-
+
-
-
-
-







	[self SSL_startTLSWithExpectedHost: host
				      port: 0];
}

- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
	       runLoopMode: (of_run_loop_mode_t)runLoopMode
		    target: (id)target
		  selector: (SEL)selector
		   context: (id)userContext
{
	void *pool = objc_autoreleasePoolPush();
	SSLSocket_ConnectContext *context;
	SSLSocket_ConnectDelegate *connectDelegate;

	context = [[[SSLSocket_ConnectContext alloc]
	    initWithHost: host
		    port: port
	connectDelegate = [[[SSLSocket_ConnectDelegate alloc]
	    initWithSocket: self
		      host: host
		      port: port
		  target: target
		selector: selector
		 context: userContext] autorelease];
		  delegate: _delegate] autorelease];
	[super asyncConnectToHost: host
			     port: port
		      runLoopMode: runLoopMode
		      runLoopMode: runLoopMode];
			   target: context
			 selector: @selector(socketDidConnect:context:
				       exception:)
			  context: nil];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_BLOCKS
- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port