设置CFStream属性失败

时间:2015-07-16 12:50:10

标签: ios ssl nsstream sni cfstream

我试图与SNI建立TLS连接。问题是第一个属性设置调用返回1表示它已被接受。以下两个返回0,这意味着他们没有通过。可能是什么原因?

在某些时候我必须添加自己的证书才能被信任,但据我了解,这将在打开流后完成,因此它不应该是任何原因。

另外,kCFStreamSocketSecurityLevelNegotiatedSSL是否支持tls1.2,因为没有常量可以直接选择?

    var tempInputStream: Unmanaged<CFReadStream>?
    var tempOutputStream: Unmanaged<CFWriteStream>?

    CFStreamCreatePairWithSocketToHost(nil, address as CFStringRef, port, &tempInputStream, &tempOutputStream)

    let cfInputStream: CFReadStream = tempInputStream!.takeRetainedValue()
    let cfOutputStream: CFWriteStream = tempOutputStream!.takeRetainedValue()

//setting properties
    print(CFReadStreamSetProperty(cfInputStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL))
    print(CFReadStreamSetProperty(cfInputStream, kCFStreamSSLValidatesCertificateChain, kCFBooleanFalse))
    print(CFReadStreamSetProperty(cfInputStream, kCFStreamSSLPeerName, "peer.address"))


    let inputStream: NSInputStream = cfInputStream
    let outputStream: NSOutputStream = cfOutputStream

    inputStream.delegate = self
    inputStream.delegate = self

    inputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
    outputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

    inputStream.open()
    outputStream.open()

1 个答案:

答案 0 :(得分:1)

kCFStreamSSLValidatesCertificateChainkCFStreamSSLPeerName不是流媒体资源。它们是SSL设置属性。您需要将它们一起收集到字典中并将其分配给kCFStreamPropertySSLSettings

let ssl = [
    String(kCFStreamSSLValidatesCertificateChain): kCFBooleanFalse, // You an probably use "false" here
    String(kCFStreamSSLPeerName): "peer.address"
]

print(CFReadStreamSetProperty(cfInputStream, kCFStreamPropertySSLSettings, ssl))