我有以下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;
}


您将要对此添加其他错误处理(以防套接字关闭等),但这应该是基本结构。

07-23 05:30