|
@@ -183,6 +183,12 @@ typedef void (^data_callback)(SRWebSocket *webSocket, NSData *data);
|
|
|
@property (nonatomic) NSOperationQueue *delegateOperationQueue;
|
|
|
@property (nonatomic) dispatch_queue_t delegateDispatchQueue;
|
|
|
|
|
|
+// Specifies whether SSL trust chain should NOT be evaluated.
|
|
|
+// By default this flag is set to NO, meaning only secure SSL connections are allowed.
|
|
|
+// For DEBUG builds this flag is ignored, and SSL connections are allowed regardless
|
|
|
+// of the certificate trust configuration
|
|
|
+@property (nonatomic, readwrite) BOOL allowsUntrustedSSLCertificates;
|
|
|
+
|
|
|
@end
|
|
|
|
|
|
|
|
@@ -227,8 +233,6 @@ typedef void (^data_callback)(SRWebSocket *webSocket, NSData *data);
|
|
|
BOOL _secure;
|
|
|
NSURLRequest *_urlRequest;
|
|
|
|
|
|
-
|
|
|
-
|
|
|
BOOL _sentClose;
|
|
|
BOOL _didFail;
|
|
|
int _closeCode;
|
|
@@ -256,13 +260,14 @@ static __strong NSData *CRLFCRLF;
|
|
|
CRLFCRLF = [[NSData alloc] initWithBytes:"\r\n\r\n" length:4];
|
|
|
}
|
|
|
|
|
|
-- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
|
|
|
+- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
|
|
|
{
|
|
|
self = [super init];
|
|
|
if (self) {
|
|
|
assert(request.URL);
|
|
|
_url = request.URL;
|
|
|
_urlRequest = request;
|
|
|
+ _allowsUntrustedSSLCertificates = allowsUntrustedSSLCertificates;
|
|
|
|
|
|
_requestedProtocols = [protocols copy];
|
|
|
|
|
@@ -272,6 +277,11 @@ static __strong NSData *CRLFCRLF;
|
|
|
return self;
|
|
|
}
|
|
|
|
|
|
+- (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
|
|
|
+{
|
|
|
+ return [self initWithURLRequest:request protocols:protocols allowsUntrustedSSLCertificates:NO];
|
|
|
+}
|
|
|
+
|
|
|
- (id)initWithURLRequest:(NSURLRequest *)request;
|
|
|
{
|
|
|
return [self initWithURLRequest:request protocols:nil];
|
|
@@ -288,9 +298,14 @@ static __strong NSData *CRLFCRLF;
|
|
|
return [self initWithURLRequest:request protocols:protocols];
|
|
|
}
|
|
|
|
|
|
+- (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
|
|
|
+{
|
|
|
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
|
|
|
+ return [self initWithURLRequest:request protocols:protocols allowsUntrustedSSLCertificates:allowsUntrustedSSLCertificates];
|
|
|
+}
|
|
|
+
|
|
|
- (void)_SR_commonInit;
|
|
|
{
|
|
|
-
|
|
|
NSString *scheme = _url.scheme.lowercaseString;
|
|
|
assert([scheme isEqualToString:@"ws"] || [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"wss"] || [scheme isEqualToString:@"https"]);
|
|
|
|
|
@@ -473,7 +488,7 @@ static __strong NSData *CRLFCRLF;
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
-- (void)didConnect
|
|
|
+- (void)didConnect;
|
|
|
{
|
|
|
SRFastLog(@"Connected");
|
|
|
CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), (__bridge CFURLRef)_url, kCFHTTPVersion1_1);
|
|
@@ -548,7 +563,12 @@ static __strong NSData *CRLFCRLF;
|
|
|
_outputStream = CFBridgingRelease(writeStream);
|
|
|
_inputStream = CFBridgingRelease(readStream);
|
|
|
|
|
|
-
|
|
|
+ _inputStream.delegate = self;
|
|
|
+ _outputStream.delegate = self;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)_updateSecureStreamOptions;
|
|
|
+{
|
|
|
if (_secure) {
|
|
|
NSMutableDictionary *SSLOptions = [[NSMutableDictionary alloc] init];
|
|
|
|
|
@@ -556,24 +576,27 @@ static __strong NSData *CRLFCRLF;
|
|
|
|
|
|
// If we're using pinned certs, don't validate the certificate chain
|
|
|
if ([_urlRequest SR_SSLPinnedCertificates].count) {
|
|
|
- [SSLOptions setValue:[NSNumber numberWithBool:NO] forKey:(__bridge id)kCFStreamSSLValidatesCertificateChain];
|
|
|
+ [SSLOptions setValue:@NO forKey:(__bridge id)kCFStreamSSLValidatesCertificateChain];
|
|
|
}
|
|
|
|
|
|
#if DEBUG
|
|
|
- [SSLOptions setValue:[NSNumber numberWithBool:NO] forKey:(__bridge id)kCFStreamSSLValidatesCertificateChain];
|
|
|
- NSLog(@"SocketRocket: In debug mode. Allowing connection to any root cert");
|
|
|
+ self.allowsUntrustedSSLCertificates = YES;
|
|
|
#endif
|
|
|
+
|
|
|
+ if (self.allowsUntrustedSSLCertificates) {
|
|
|
+ [SSLOptions setValue:@NO forKey:(__bridge id)kCFStreamSSLValidatesCertificateChain];
|
|
|
+ SRFastLog(@"Allowing connection to any root cert");
|
|
|
+ }
|
|
|
|
|
|
[_outputStream setProperty:SSLOptions
|
|
|
forKey:(__bridge id)kCFStreamPropertySSLSettings];
|
|
|
}
|
|
|
-
|
|
|
- _inputStream.delegate = self;
|
|
|
- _outputStream.delegate = self;
|
|
|
}
|
|
|
|
|
|
- (void)openConnection;
|
|
|
{
|
|
|
+ [self _updateSecureStreamOptions];
|
|
|
+
|
|
|
if (!_scheduledRunloops.count) {
|
|
|
[self scheduleInRunLoop:[NSRunLoop SR_networkRunLoop] forMode:NSDefaultRunLoopMode];
|
|
|
}
|