从 iOS 10.3 之前的 SecCertificate 获取 SecKey

Get SecKey from SecCertificate pre iOS 10.3

我需要从证书

中获取public密钥

我找到了在 iOS 12+ 和 iOS 10.3+ 上执行此操作的方法,但如何在 iOS 10.0+ 上执行此操作?

    func publicKey(for certificate: SecCertificate) -> SecKey? {
        if #available(iOS 12.0, *) {
            return SecCertificateCopyKey(certificate)
        } else if #available(iOS 10.3, *) {
            return SecCertificateCopyPublicKey(certificate)
        } else {
            // ???
            return nil
        }
    }

我找到了解决方案

func publicKey(for certificate: SecCertificate) -> SecKey? {
    if #available(iOS 12.0, *) {
        return SecCertificateCopyKey(certificate)
    } else if #available(iOS 10.3, *) {
        return SecCertificateCopyPublicKey(certificate)
    } else {
        var possibleTrust: SecTrust?
        SecTrustCreateWithCertificates(certificate, SecPolicyCreateBasicX509(), &possibleTrust)
        guard let trust = possibleTrust else { return nil }
        var result: SecTrustResultType = .unspecified
        SecTrustEvaluate(trust, &result)
        return SecTrustCopyPublicKey(trust)
    }
}

在此处找到:https://github.com/daltoniam/Starscream/blob/a2ed45c0b2f996cb8c335c4f270ecc68c3bd4c0f/Sources/Starscream/SSLSecurity.swift#L214