gpt4 book ai didi

ios - GCDAsyncSocket startTLS 不起作用

转载 作者:行者123 更新时间:2023-12-01 18:08:43 61 4
gpt4 key购买 nike

我能够通过 IP 和端口连接到所需的套接字。

- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port {

[sender startTLS:nil];

if(self.createTCPSocketHelperDelegate && @selector(returnConnectedTCPSocket:forNASWithMacAddress:))
{
[self.createTCPSocketHelperDelegate returnConnectedTCPSocket:sender forNASWithMacAddress:_macAddress];
}
}

我正在发送 TLS 设置字典 nil,因为它使用默认设置。它给了我如下错误

Error Domain=kCFStreamErrorDomainSSL Code=-9806 "The operation couldn’t be completed. (kCFStreamErrorDomainSSL error -9806.)" UserInfo=0x17d92420 {NSLocalizedRecoverySuggestion=Error code definition can be found in Apple's SecureTransport.h}

我不明白那里发生了什么问题。请帮我提供一些连接到SSL服务器并使用TCP/TLS协议(protocol)的示例代码。

如果有帮助,我们将不胜感激。先感谢您。 :)

最佳答案

我遇到了这个问题。基本上我无法完成SSL握手,我找到了如下方法:

初始化GCDAsyncSocket套接字并连接到IP和端口,如下所示,

NSError *error;

_gcdAsyncTCPSocket = [[GCDAsyncSocket alloc] initWithDelegate:self
delegateQueue:dispatch_get_main_queue()];

_gcdAsyncTCPSocket.delegate = self;

_ipAddress = ipAddress;

_tcpPortNumber = portNumber;

if(![_gcdAsyncTCPSocket connectToHost:ipAddress onPort:portNumber withTimeout:30 error:&error])
{
if(self.createTCPSocketHelperDelegate && @selector(failedToCreateTCPSocket))
{
[self.createTCPSocketHelperDelegate failedToCreateTCPSocket];
}
}else {

NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];

[settings setObject:[NSNumber numberWithBool:YES]
forKey:GCDAsyncSocketManuallyEvaluateTrust];

[_gcdAsyncTCPSocket startTLS:settings];
}

当您设置 GCDAsyncSocketManuallyEvaluateTrust 时,它会调用委托(delegate)方法

- (void)socket:(GCDAsyncSocket *)sock didReceiveTrust:(SecTrustRef)trust completionHandler:(void (^)(BOOL shouldTrustPeer))completionHandler

其中我使用本地证书评估了服务器的自签名证书。如果相等则completionHandler(YES),否则completionHandler(NO)如下:

- (void)socket:(GCDAsyncSocket *)sock didReceiveTrust:(SecTrustRef)trust completionHandler:(void (^)(BOOL shouldTrustPeer))completionHandler {
NSLog(@"didReceiveTrust");

//server certificate
SecCertificateRef serverCertificate = SecTrustGetCertificateAtIndex(trust, 0);
CFDataRef serverCertificateData = SecCertificateCopyData(serverCertificate);

const UInt8* const serverData = CFDataGetBytePtr(serverCertificateData);
const CFIndex serverDataSize = CFDataGetLength(serverCertificateData);
NSData* cert1 = [NSData dataWithBytes:serverData length:(NSUInteger)serverDataSize];


//local certificate
NSString *localCertFilePath = [[NSBundle mainBundle] pathForResource:@"LocalCertificate" ofType:@"cer"];
NSData *localCertData = [NSData dataWithContentsOfFile:localCertFilePath];
CFDataRef myCertData = (__bridge CFDataRef)localCertData;


const UInt8* const localData = CFDataGetBytePtr(myCertData);
const CFIndex localDataSize = CFDataGetLength(myCertData);
NSData* cert2 = [NSData dataWithBytes:localData length:(NSUInteger)localDataSize];


if (cert1 == nil || cert2 == nil) {
NSLog(@"Certificate NULL");
completionHandler(NO);
return;
}


const BOOL equal = [cert1 isEqualToData:cert2];

if (equal) {

NSLog(@"Certificate match");
completionHandler(YES);

}else{

NSLog(@"Certificate not match");
completionHandler(NO);
}
}

如果证书与completionHandler(YES)匹配,它将调用委托(delegate)

- (void)socketDidSecure:(GCDAsyncSocket *)sock {

NSLog(@"socketDidSecure");

_uploadSocket = sock;

//Do your stuff after this
}

希望它能帮助某人处理 SSL 和 GCDAsyncSocket。快乐编码!!

关于ios - GCDAsyncSocket startTLS 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050743/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com