【问题标题】:iOS: SocketRocket - How to implement SSL HandshakeiOS:SocketRocket - 如何实现 SSL 握手
【发布时间】:2014-10-21 15:59:59
【问题描述】:
刚刚切换到 Websockets 与 Protobufs 的结合。像 IOS 上的魅力一样工作,但我不确定如何通过 SocketRocket Lib 实现 SSL 握手(如使用 NSURLConnection)。有人有这方面的经验,或者它还不支持。
TSL 连接已经在工作,SSL pinning 也可以工作 - 但是如何通过 SocketRocket 使用 Web 套接字正确验证 SSL 链来实现正确的 SSL 握手?!
BR
【问题讨论】:
标签:
ios
ssl
nsurlconnection
socketrocket
【解决方案1】:
编辑:纠正我之前回答中的错误。
Socket Rocket 在后台使用的 CFStream 将在证书已添加到钥匙串的情况下自动处理握手。如果您需要添加证书,请参阅此问题的答案:iOS: Pre install SSL certificate in keychain - programmatically
但是,如果您正在寻找 Pinning,那么使用 Socket Rocket 即可轻松实现。使用 initWithURLRequest 初始化程序,其他一切都会自动处理。对于固定证书,SocketRocket 不会验证您想要的行为的证书链,因为通过固定,您明确表示信任此证书或仅由此证书签名的证书。即它不依赖于验证链。
NSURL *url = [NSURL URLWithString: ServerSocketURLString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"certificatefilename" ofType:@"cer"];
NSData *certData = [[NSData alloc] initWithContentsOfFile:cerPath];
CFDataRef certDataRef = (__bridge CFDataRef)certData;
SecCertificateRef certRef = SecCertificateCreateWithData(NULL, certDataRef);
id certificate = (__bridge id)certRef;
[request setSR_SSLPinnedCertificates:@[certificate]];
self.clientWebSocket = [[SRWebSocket alloc] initWithURLRequest:request];
self.clientWebSocket.delegate = self;