苹果安全传输代码错误

apple secure transport code error

本文关键字:错误 代码 传输 安全 苹果      更新时间:2023-10-16

我有以下Apple Security API初始化代码:

    OSStatus status = noErr;
    sslContext = SSLCreateContext(kCFAllocatorDefault, kSSLClientSide, kSSLStreamType);
    if (sslContext == nullptr)
    {
        throw std::runtime_error("cannot create ssl context");
    }
    status = SSLSetIOFuncs(sslContext, socketRead, socketWrite);
    if (status != noErr)
    {
        throw std::runtime_error("cannot set ssl io functions");
    }
    status = SSLSetConnection(sslContext, reinterpret_cast<void*>(&handle));
    if (status != noErr)
    {
        throw std::runtime_error("cannot set ssl connections");
    }
    status = SSLSetPeerDomainName(sslContext, address.c_str(), address.length());
    if (status != noErr)
    {
        throw std::runtime_error("cannot set ssl peer domain name");
    }
    status = SSLSetSessionOption(sslContext, kSSLSessionOptionBreakOnServerAuth, true);
    if (status != noErr)
    {
        throw std::runtime_error("cannot set ssl options");
    }
    status = SSLHandshake(sslContext);
    if (status != noErr)
    {
        throw std::runtime_error("cannot perform ssl handshake");
    }

在这一行上:

status = SSLHandshake(sslContext);

我的SSLWriteFunc回调被调用。它尝试通过 SSLWrite 发送 174 字节(我不是故意发送它(,但无论如何它都失败了。但是在关于SSLHandshake的文档中,它写道:"成功返回后,会话已准备好使用函数SSLRead(::::)进行正常的安全通信。和 SSLWrite(:::).".所以在我的情况下,这种方法没有返回,然后我的SSLWriteFunc突然尝试通过SSLWrite发送数据。我只是不明白我做错了什么。请伙计们帮助我。

SSLRead()SSLWrite()应该从应用程序代码中调用,而不是从SSLReadFuncSSLWriteFunc回调内部调用。

SSLReadFuncSSLWriteFunc回调中,SecureTransport 要求您从套接字接收/发送数据(您使用 SSLSetConnection 设置(。因此,在SSLWriteFunc,您将获得加密数据以通过套接字发送。SSLWriteFunc的实现应如下所示:

OSStatus mySSLWriteFunc(SSLConnectionRef connection, const void *data, size_t *dataLength)
{
    int *handle;
    SSLGetConnection(connection, &handle);
    size_t result = write(*handle, data, *dataLength);
    if (result == -1)
    {
        if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
            return errSSLWouldBlock;
        else
            // fill this in
    }
    else
    {
        *dataLength = result;
    }
    return noErr;
}

您需要为此添加额外的错误处理(以防套接字关闭等(,但这应该是基本结构。