【发布时间】:2015-05-19 06:07:56
【问题描述】:
我需要在UIWebView 中显示所显示 URL 的 SSL 证书详细信息,类似于 Google 的 Chrome 浏览器显示:
如何从UIWebView获取这些数据。
【问题讨论】:
-
@utility 没什么,我检查了
UIWebView中的方法,什么也没找到
标签: ios ssl uiwebview ssl-certificate
我需要在UIWebView 中显示所显示 URL 的 SSL 证书详细信息,类似于 Google 的 Chrome 浏览器显示:
如何从UIWebView获取这些数据。
【问题讨论】:
UIWebView 中的方法,什么也没找到
标签: ios ssl uiwebview ssl-certificate
我们在网络级别(而不是 UIWebView)拦截调用,并使用[NSURLConnectionDelegate connection:willSendRequestForAuthenticationChallenge:]。这会给你一个NSURLAuthenticationChallenge 实例,如果challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust 那么NSURLAuthenticationChallenge.protectionSpace.serverTrust 是一个SecTrustRef。
鉴于SecTrustRef,您可以关注SecCertificateRef: How to get the certificate information? 并执行以下操作:
#import <Security/Security.h>
#import <openssl/x509.h>
X509* X509CertificateFromSecTrust(SecTrustRef trust) {
SecCertificateRef cert = SecTrustGetCertificateAtIndex(trust, 0);
CFDataRef certDataRef = SecCertificateCopyData(cert);
NSData *certData = (__bridge NSData *)certDataRef;
const void* certDataBytes = certData.bytes;
X509* result = d2i_X509(NULL, (const unsigned char**)&certDataBytes, certData.length);
CFRelease(certDataRef);
return result;
}
static NSString* X509NameField(X509_NAME* name, char* key) {
if (name == NULL)
return nil;
int nid = OBJ_txt2nid(key);
int index = X509_NAME_get_index_by_NID(name, nid, -1);
X509_NAME_ENTRY *nameEntry = X509_NAME_get_entry(name, index);
if (nameEntry == NULL)
return nil;
ASN1_STRING *nameASN1 = X509_NAME_ENTRY_get_data(nameEntry);
if (nameASN1 == NULL)
return nil;
unsigned char *issuerName = ASN1_STRING_data(nameASN1);
return [NSString stringWithUTF8String:(char *)issuerName];
}
NSString* X509CertificateGetSubjectCommonName(X509* cert) {
if (cert == NULL)
return nil;
X509_NAME *subjectName = X509_get_subject_name(cert);
return X509NameField(subjectName, "CN"); // Common name.
}
NSString* X509CertificateGetIssuerName(X509* certX509) {
if (certX509 == NULL)
return nil;
X509_NAME *issuerX509Name = X509_get_issuer_name(certX509);
if (issuerX509Name == NULL)
return nil;
return X509NameField(issuerX509Name, "O"); // organization
}
这不是一件简单的工作。您需要了解 OpenSSL 的 X509 代码、安全框架以及您在网络层为 SSL 信任检查所做的任何事情。可能还有其他方法可以获取 SecTrustRef 或 SecCertificateRef,但如果有的话,我不会使用它们。
【讨论】: