Skip to content

Commit

Permalink
Split creation of random data into SRRandom.
Browse files Browse the repository at this point in the history
  • Loading branch information
nlutsenko committed Jun 17, 2016
1 parent d3beb35 commit 86df623
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
20 changes: 20 additions & 0 deletions SocketRocket.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@
81C22BC71D124168007BFDDF /* SRHTTPConnectMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 81C22BC11D124168007BFDDF /* SRHTTPConnectMessage.m */; };
81C22BC81D124168007BFDDF /* SRHTTPConnectMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 81C22BC11D124168007BFDDF /* SRHTTPConnectMessage.m */; };
81C22BC91D124168007BFDDF /* SRHTTPConnectMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 81C22BC11D124168007BFDDF /* SRHTTPConnectMessage.m */; };
81C22BF81D1256E1007BFDDF /* SRRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = 81C22BF61D1256E1007BFDDF /* SRRandom.h */; };
81C22BF91D1256E1007BFDDF /* SRRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = 81C22BF61D1256E1007BFDDF /* SRRandom.h */; };
81C22BFA1D1256E1007BFDDF /* SRRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = 81C22BF61D1256E1007BFDDF /* SRRandom.h */; };
81C22BFB1D1256E1007BFDDF /* SRRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = 81C22BF61D1256E1007BFDDF /* SRRandom.h */; };
81C22BFC1D1256E1007BFDDF /* SRRandom.m in Sources */ = {isa = PBXBuildFile; fileRef = 81C22BF71D1256E1007BFDDF /* SRRandom.m */; };
81C22BFD1D1256E1007BFDDF /* SRRandom.m in Sources */ = {isa = PBXBuildFile; fileRef = 81C22BF71D1256E1007BFDDF /* SRRandom.m */; };
81C22BFE1D1256E1007BFDDF /* SRRandom.m in Sources */ = {isa = PBXBuildFile; fileRef = 81C22BF71D1256E1007BFDDF /* SRRandom.m */; };
81C22BFF1D1256E1007BFDDF /* SRRandom.m in Sources */ = {isa = PBXBuildFile; fileRef = 81C22BF71D1256E1007BFDDF /* SRRandom.m */; };
81CD05D71CEEC47300497F47 /* NSURLRequest+SRWebSocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 81CD05D51CEEC47300497F47 /* NSURLRequest+SRWebSocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
81CD05D81CEEC47300497F47 /* NSURLRequest+SRWebSocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 81CD05D51CEEC47300497F47 /* NSURLRequest+SRWebSocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
81CD05D91CEEC47300497F47 /* NSURLRequest+SRWebSocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 81CD05D51CEEC47300497F47 /* NSURLRequest+SRWebSocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -197,6 +205,8 @@
81B31C5E1CDC444900D86D43 /* SRRunLoopThread.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SRRunLoopThread.m; sourceTree = "<group>"; };
81C22BC01D124168007BFDDF /* SRHTTPConnectMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SRHTTPConnectMessage.h; sourceTree = "<group>"; };
81C22BC11D124168007BFDDF /* SRHTTPConnectMessage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SRHTTPConnectMessage.m; sourceTree = "<group>"; };
81C22BF61D1256E1007BFDDF /* SRRandom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SRRandom.h; sourceTree = "<group>"; };
81C22BF71D1256E1007BFDDF /* SRRandom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SRRandom.m; sourceTree = "<group>"; };
81CD05D51CEEC47300497F47 /* NSURLRequest+SRWebSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURLRequest+SRWebSocket.h"; sourceTree = "<group>"; };
81CD05D61CEEC47300497F47 /* NSURLRequest+SRWebSocket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURLRequest+SRWebSocket.m"; sourceTree = "<group>"; };
81CD05FB1CEEC65D00497F47 /* NSRunLoop+SRWebSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSRunLoop+SRWebSocket.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -406,6 +416,8 @@
children = (
81B31C2B1CDC406B00D86D43 /* SRHash.h */,
81B31C2C1CDC406B00D86D43 /* SRHash.m */,
81C22BF61D1256E1007BFDDF /* SRRandom.h */,
81C22BF71D1256E1007BFDDF /* SRRandom.m */,
81B22EE21CE43ECC0073C636 /* SRURLUtilities.h */,
81B22EE31CE43ECC0073C636 /* SRURLUtilities.m */,
81C22BC01D124168007BFDDF /* SRHTTPConnectMessage.h */,
Expand Down Expand Up @@ -546,6 +558,7 @@
2D42277F1BB4365C000C1A6C /* SRWebSocket.h in Headers */,
81B31C2E1CDC406B00D86D43 /* SRHash.h in Headers */,
811934BE1CDAF725003AB243 /* SocketRocket.h in Headers */,
81C22BF91D1256E1007BFDDF /* SRRandom.h in Headers */,
81C22BC31D124168007BFDDF /* SRHTTPConnectMessage.h in Headers */,
817995871CE139700084DA37 /* SRDelegateController.h in Headers */,
81B22EC61CE42D7E0073C636 /* SRError.h in Headers */,
Expand All @@ -567,6 +580,7 @@
3345DC8A1C52ACD70083CCB8 /* SRWebSocket.h in Headers */,
81B31C301CDC406B00D86D43 /* SRHash.h in Headers */,
811934C01CDAF726003AB243 /* SocketRocket.h in Headers */,
81C22BFB1D1256E1007BFDDF /* SRRandom.h in Headers */,
81C22BC51D124168007BFDDF /* SRHTTPConnectMessage.h in Headers */,
817995891CE139700084DA37 /* SRDelegateController.h in Headers */,
81B22EC81CE42D7E0073C636 /* SRError.h in Headers */,
Expand All @@ -588,6 +602,7 @@
F668C8AA153E92F90044DBAC /* SRWebSocket.h in Headers */,
81B31C2F1CDC406B00D86D43 /* SRHash.h in Headers */,
811934BC1CDAF725003AB243 /* SocketRocket.h in Headers */,
81C22BFA1D1256E1007BFDDF /* SRRandom.h in Headers */,
81C22BC41D124168007BFDDF /* SRHTTPConnectMessage.h in Headers */,
817995881CE139700084DA37 /* SRDelegateController.h in Headers */,
81B22EC71CE42D7E0073C636 /* SRError.h in Headers */,
Expand All @@ -609,6 +624,7 @@
81B31C2D1CDC406B00D86D43 /* SRHash.h in Headers */,
4861E7751D022211002FAB1D /* SRProxyConnect.h in Headers */,
555E0EB41C51E57A00E6BB92 /* SocketRocket.h in Headers */,
81C22BF81D1256E1007BFDDF /* SRRandom.h in Headers */,
81C22BC21D124168007BFDDF /* SRHTTPConnectMessage.h in Headers */,
817995861CE139700084DA37 /* SRDelegateController.h in Headers */,
81B22EC51CE42D7E0073C636 /* SRError.h in Headers */,
Expand Down Expand Up @@ -810,6 +826,7 @@
81C22BC71D124168007BFDDF /* SRHTTPConnectMessage.m in Sources */,
81CD06021CEEC65D00497F47 /* NSRunLoop+SRWebSocket.m in Sources */,
2D4227851BB43734000C1A6C /* SRWebSocket.m in Sources */,
81C22BFD1D1256E1007BFDDF /* SRRandom.m in Sources */,
81B31C211CDC404100D86D43 /* SRIOConsumerPool.m in Sources */,
81B22EE91CE43ECC0073C636 /* SRURLUtilities.m in Sources */,
8133640C1D091E1B0062E28D /* SRProxyConnect.m in Sources */,
Expand All @@ -830,6 +847,7 @@
81C22BC91D124168007BFDDF /* SRHTTPConnectMessage.m in Sources */,
81CD06041CEEC65D00497F47 /* NSRunLoop+SRWebSocket.m in Sources */,
3345DC841C52ACD70083CCB8 /* SRWebSocket.m in Sources */,
81C22BFF1D1256E1007BFDDF /* SRRandom.m in Sources */,
81B31C231CDC404100D86D43 /* SRIOConsumerPool.m in Sources */,
81B22EEB1CE43ECC0073C636 /* SRURLUtilities.m in Sources */,
8133640F1D091E1C0062E28D /* SRProxyConnect.m in Sources */,
Expand Down Expand Up @@ -861,6 +879,7 @@
81C22BC81D124168007BFDDF /* SRHTTPConnectMessage.m in Sources */,
81CD06031CEEC65D00497F47 /* NSRunLoop+SRWebSocket.m in Sources */,
F6396B86153E67EC00345B5E /* SRWebSocket.m in Sources */,
81C22BFE1D1256E1007BFDDF /* SRRandom.m in Sources */,
81B31C221CDC404100D86D43 /* SRIOConsumerPool.m in Sources */,
81B22EEA1CE43ECC0073C636 /* SRURLUtilities.m in Sources */,
8133640E1D091E1B0062E28D /* SRProxyConnect.m in Sources */,
Expand All @@ -881,6 +900,7 @@
81C22BC61D124168007BFDDF /* SRHTTPConnectMessage.m in Sources */,
81B31C181CDC404100D86D43 /* SRIOConsumer.m in Sources */,
81CD06011CEEC65D00497F47 /* NSRunLoop+SRWebSocket.m in Sources */,
81C22BFC1D1256E1007BFDDF /* SRRandom.m in Sources */,
F6A12CD2145119B700C1D980 /* SRWebSocket.m in Sources */,
81B31C201CDC404100D86D43 /* SRIOConsumerPool.m in Sources */,
81B22EE81CE43ECC0073C636 /* SRURLUtilities.m in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions SocketRocket/Internal/Utilities/SRRandom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

extern NSData *SRRandomData(NSUInteger length);

NS_ASSUME_NONNULL_END
26 changes: 26 additions & 0 deletions SocketRocket/Internal/Utilities/SRRandom.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright (c) 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//

#import "SRRandom.h"

#import <Security/SecRandom.h>

NS_ASSUME_NONNULL_BEGIN

NSData *SRRandomData(NSUInteger length)
{
NSMutableData *data = [NSMutableData dataWithLength:length];
int result = SecRandomCopyBytes(kSecRandomDefault, data.length, data.mutableBytes);
if (result != 0) {
[NSException raise:NSInternalInconsistencyException format:@"Failed to generate random bytes with OSStatus: %d", result];
}
return data;
}

NS_ASSUME_NONNULL_END
18 changes: 2 additions & 16 deletions SocketRocket/SRWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#import <CoreServices/CoreServices.h>
#endif

#import <Security/SecRandom.h>

#import "SRDelegateController.h"
#import "SRIOConsumer.h"
#import "SRIOConsumerPool.h"
Expand All @@ -38,6 +36,7 @@
#import "SRProxyConnect.h"
#import "SRSecurityOptions.h"
#import "SRHTTPConnectMessage.h"
#import "SRRandom.h"

#if !__has_feature(objc_arc)
#error SocketRocket must be compiled with ARC enabled
Expand Down Expand Up @@ -395,21 +394,8 @@ - (void)_readHTTPHeader;
- (void)didConnect;
{
SRFastLog(@"Connected");
NSMutableData *keyBytes = [[NSMutableData alloc] initWithLength:16];
int result = SecRandomCopyBytes(kSecRandomDefault, keyBytes.length, keyBytes.mutableBytes);
if (result != 0) {
//TODO: (nlutsenko) Check if there was an error.
}

if ([keyBytes respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
_secKey = [keyBytes base64EncodedStringWithOptions:0];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
_secKey = [keyBytes base64Encoding];
#pragma clang diagnostic pop
}

_secKey = SRBase64EncodedStringFromData(SRRandomData(16));
assert([_secKey length] == 24);

CFHTTPMessageRef message = SRHTTPConnectMessageCreate(_urlRequest,
Expand Down

0 comments on commit 86df623

Please sign in to comment.