gpt4 book ai didi

iOS 和 Erlang - 使用自签名证书的 SSL 握手失败

转载 作者:可可西里 更新时间:2023-11-01 06:06:22 26 4
gpt4 key购买 nike

我正在尝试通过 TCP/IP 创建从 iOS 到 Erlang 服务器的 SSL 连接。服务器正在使用自签名证书(Erlang OTP 附带的默认证书)。

每次 SSL 握手失败:- CFNetwork SSLHandshake 失败(-9824 -> -9829)- CFNetwork SSLHandshake 失败 (-9807)

这是我使用的一种方法:

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"my-server-name", 1234, &readStream, &writeStream);

//------------------------------------------------------
// Set props.
//
CFReadStreamSetProperty(readStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL);
CFReadStreamSetProperty(readStream, kCFStreamSSLAllowsExpiredCertificates, kCFBooleanTrue);
CFReadStreamSetProperty(readStream, kCFStreamSSLAllowsExpiredRoots, kCFBooleanTrue);
CFReadStreamSetProperty(readStream, kCFStreamSSLAllowsAnyRoot, kCFBooleanTrue);
CFReadStreamSetProperty(readStream, kCFStreamSSLValidatesCertificateChain, kCFBooleanFalse);
CFReadStreamSetProperty(readStream, kCFStreamSSLPeerName, kCFNull);

CFWriteStreamSetProperty(writeStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL);
CFWriteStreamSetProperty(writeStream, kCFStreamSSLAllowsExpiredCertificates, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamSSLAllowsExpiredRoots, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamSSLAllowsAnyRoot, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamSSLValidatesCertificateChain, kCFBooleanFalse);
CFWriteStreamSetProperty(writeStream, kCFStreamSSLPeerName, kCFNull);

//------------------------------------------------------
// Open streams.
//
if(!CFReadStreamOpen(readStream))
{
NSLog(@"CFReadStreamOpen Failed!");
return 0;
}

if(!CFWriteStreamOpen(writeStream))
{
NSLog(@"CFWriteStreamOpen Failed!");
CFReadStreamClose(readStream);
return 0;
}

//------------------------------------------------------
// Send some data.
//
UInt8 data[20] = {0};

*(ushort*)data = 18;
for(int i = 2; i < 20; i++)
{
data[i] = 'A' + i;
}

NSLog(@"Sending some data...");

CFIndex bytesSent = CFWriteStreamWrite(writeStream, data, 20);
NSLog(@"Bytes Sent: %d", (int)bytesSent);

//------------------------------------------------------
// Close streams.
//
CFReadStreamClose(readStream);
CFWriteStreamClose(writeStream);

我也尝试使用 SecureTransport(SSLCreateContext、SSLSetConnection、SSLHandshake,它们是一样的 - SSL 握手失败。

非常感谢任何建议。

最佳答案

错误的可能原因有很多。您可能应该首先在 Mac 上使用 curl 命令来访问 Erlang 服务器。 Curl 会给你更多的描述性错误。您需要传递 -k 标志以告诉它忽略证书错误。

一些快速的可能性:您的 Objective-C 代码是否设置为允许无效证书?是否设置为验证域名,并且您的开发设置不执行反向 DNS?

关于iOS 和 Erlang - 使用自签名证书的 SSL 握手失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362749/

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