从 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)
}
}
我需要从证书
中获取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)
}
}