我正在使用 Starscream 套接字库并尝试使用 WSS,但是我遇到了握手失败。我得到了我的自签名证书,我将其转换为 .der 文件。这是我正在尝试的代码
var socket = WebSocket(url: URL(string: "wss://192.168.1.130:6223")!, protocols: [])
override func viewDidLoad() {
super.viewDidLoad()
do
{
let urlPath = Bundle.main.path(forResource: "my_cert", ofType: "der")
let url = NSURL.fileURL(withPath: urlPath!)
let certificateData = try Data(contentsOf: url)
let certificate: SecCertificate =
SecCertificateCreateWithData(kCFAllocatorDefault, certificateData as CFData)!
var trust: SecTrust?
let policy = SecPolicyCreateBasicX509()
let status = SecTrustCreateWithCertificates(certificate, policy, &trust)
if status == errSecSuccess {
let key = SecTrustCopyPublicKey(trust!)!;
let ssl = SSLCert(key: key)
socket.security = SSLSecurity(certs: [ssl], usePublicKeys: true)
socket.delegate = self
socket.connect()
}
}catch let error as NSError
{
print(error)
}
}
所以当我尝试连接时,我收到以下错误消息
2017-07-07 11:06:26.590 CertificateTesting[5180:81661] CFNetwork SSLHandshake failed (-9807) websocket is disconnected: 操作无法完成。(OSStatus 错误 -9807。)
证书应该可以正常工作,我的 Android 同事已经在他身边尝试过,没有任何问题。我可以让它在我身边工作的唯一方法是如果我像这样禁用 SSL 验证
socket.disableSSLCertValidation = true
有没有人有使用带有套接字的自签名 SSL 的经验。任何信息将不胜感激。
编辑:
我调用了 verify ssl 命令,它返回
➜ CertificateTesting git:(master) ✗ openssl verify -my_cert.der ca-cert.pem server-cert.pem
usage: verify [-verbose] [-CApath path] [-CAfile file] [-purpose purpose] [-crl_check] [-engine e] cert1 cert2 ...
recognized usages:
sslclient SSL client
sslserver SSL server
nssslserver Netscape SSL server
smimesign S/MIME signing
smimeencrypt S/MIME encryption
crlsign CRL signing
any Any Purpose
ocsphelper OCSP helper
这看起来好吗?