【问题标题】:apple secure transport code error苹果安全传输代码错误
【发布时间】:2017-06-26 14:35:45
【问题描述】:

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

【问题讨论】:

    标签: c++ macos ssl secure-transport


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2018-05-01
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2011-02-02
    相关资源
    最近更新 更多