0

我有以下 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 发送数据。我只是不明白我做错了什么。请大家帮帮我。

4

1 回答 1

0

SSLRead()并且SSLWrite()应该从您的应用程序代码中调用,而不是从SSLReadFuncandSSLWriteFunc回调中调用。

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;
}

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

于 2017-02-09T17:51:06.493 回答