gpt4 book ai didi

ios - Xcode 中的证书固定

转载 作者:可可西里 更新时间:2023-11-01 03:58:47 24 4
gpt4 key购买 nike

我得到了以下证书代码 pinning in Android

CertificatePinner certificatePinner = new CertificatePinner.Builder()
.add("publicobject.com", "sha1/DmxUShsZuNiqPQsX2Oi9uv2sCnw=")
.add("publicobject.com", "sha1/SXxoaOSEzPC6BgGmxAt/EAcsajw=")
.add("publicobject.com", "sha1/blhOM3W9V/bVQhsWAcLYwPU6n24=")
.add("publicobject.com", "sha1/T5x9IXmcrQ7YuQxXnxoCmeeQ84c=")
.build();

如何使用 NSURLSession 方法在 IOS 中完成相同的任务?

这里有一些引用代码

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, 0);
NSData *remoteCertificateData = CFBridgingRelease(SecCertificateCopyData(certificate));
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"MyLocalCertificate" ofType:@"cer"];
NSData *localCertData = [NSData dataWithContentsOfFile:cerPath];
if ([remoteCertificateData isEqualToData:localCertData]) {
NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}

编辑部分

我得到了以下解决方案,在 NSURLSession 中自动调用了哪个委托(delegate)函数,谁能解释一下它是如何工作的?还需要发送乘数证书我该怎么做?

 (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
NSString *authMethod = [[challenge protectionSpace] authenticationMethod];

if ([authMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
} else {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, 0);
NSData *remoteCertificateData = CFBridgingRelease(SecCertificateCopyData(certificate));
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"MyLocalCertificate" ofType:@"cer"];
NSData *localCertData = [NSData dataWithContentsOfFile:cerPath];
NSURLCredential *credential;

if ([remoteCertificateData isEqualToData:localCertData]) {
credential = [NSURLCredential credentialForTrust:serverTrust];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}



completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
NSLog(@"Finished Challenge");
}
}

最佳答案

如果身份验证方法是 NSURLAuthenticationMethodServerTrust,if block 会跳过证书锁定。我不太确定您为什么要这样做——您必须查看您获得此代码片段的源代码并了解它的要求。

如果身份验证方法是其他任何方法,则 else block 执行证书固定。

变量 serverTrust 从服务器发送到 SSL 事务状态。这里最主要的是它有一个验证服务器的证书链。在下一行中,certificate 被设置为链中的叶证书,即服务器的证书。

remoteCertificateData 本质上是一个大的二进制 blob,表示证书中的信息。内存管理需要调用CFBridgingRelease(所有CFxxx函数都是C/C++函数,不是Objective-C,内存管理稍微复杂一点正常)。

localCertData 是证书本地副本中信息的二进制 blob。请注意,iOS 应用程序(或多或少)是一个文件集合,包括可执行文件以及各种资源等。作为构建过程的一部分,您可以安排将服务器证书的副本包含在这些集合中(NSBundle) 的文件。 cerPath 变量设置为证书本地副本的文件路径。

最后,我们检查两个二进制 blob 是否相等。如果不是,则来自服务器的证书是伪造的,我们不会继续处理该请求。

我不完全确定“需要发送乘数证书”是什么意思。从您引用的 Java 代码来看,我假设您的意思是要将服务器证书与多个本地证书进行比较。在这种情况下,(大致)类似于以下内容(注意:未经测试的代码):

   SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, 0);
NSData *remoteCertificateData = CFBridgingRelease(SecCertificateCopyData(certificate));

BOOL match = NO;
NSURLCredential *credential;

for (NSString *path in [[NSBundle mainBundle] pathsForResourcesOfType:@"cer" inDirectory:@"."]) {
NSData *localCertData = [NSData dataWithContentsOfFile:path];

if ([remoteCertificateData isEqualToData:localCertData]) {
credential = [NSURLCredential credentialForTrust:serverTrust];
match = YES;
break;
}
}

if (match) {
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}

completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
NSLog(@"Finished Challenge");

关于ios - Xcode 中的证书固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41260301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com