gpt4 book ai didi

iphone - 无法在 iPhone 中使用客户端证书进行身份验证

转载 作者:行者123 更新时间:2023-12-03 18:47:19 25 4
gpt4 key购买 nike

团队,

我有基于 .net 的 REST 服务,配置了 2 路 SSL。在我的 iPhone 端,我已在设备配置文件中安装了服务器证书,并且客户端证书作为应用程序资源捆绑在一起。服务器证书验证工作正常,但客户端证书验证失败。下面是我的代码片段

- (void)connection:(NSURLConnection *) connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"Authentication challenge with host: %@", challenge.protectionSpace.host);

if([challenge previousFailureCount] == 0) {
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
NSString *authMethod = [protectionSpace authenticationMethod];
if(authMethod == NSURLAuthenticationMethodServerTrust ) {
NSLog(@"Verifying The Trust");
[[challenge sender] useCredential:[NSURLCredential credentialForTrust:[protectionSpace serverTrust]] forAuthenticationChallenge:challenge];
}
else if(authMethod == NSURLAuthenticationMethodClientCertificate ) {
NSLog(@"Trying Certificate");
// load cert

NSString *thePath = [[NSBundle mainBundle]
pathForResource:@"Myclientcertificate" ofType:@"pfx"];
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
CFDataRef inPKCS12Data = (CFDataRef)PKCS12Data;

OSStatus status = noErr;
SecIdentityRef myIdentity;
SecTrustRef myTrust;

status = extractIdentityAndTrust(
inPKCS12Data,
&myIdentity,
&myTrust);

SecTrustResultType trustResult;

if (status == noErr) {
status = SecTrustEvaluate(myTrust, &trustResult);
}

SecCertificateRef myCertificate;
SecIdentityCopyCertificate(myIdentity, &myCertificate);
const void *certs[] = { myCertificate };
CFArrayRef certsArray = CFArrayCreate(NULL, certs, 1, NULL);


NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity certificates:(NSArray*)certsArray persistence:NSURLCredentialPersistencePermanent];

[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];


}
}

}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
BOOL result;
NSLog(@"canAuthenticateAgainstProtectionSpace: %@", protectionSpace.authenticationMethod);
if ([protectionSpace authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
result= YES;
} else if([protectionSpace authenticationMethod] == NSURLAuthenticationMethodClientCertificate) {
result = YES;
}
return result;
}

OSStatus extractIdentityAndTrust(CFDataRef inPKCS12Data, SecIdentityRef *identity, SecTrustRef *trust){
OSStatus securityError = errSecSuccess;


CFStringRef password = CFSTR("1234");
const void *keys[] = { kSecImportExportPassphrase };
const void *values[] = { password };
CFDictionaryRef optionsDictionary = CFDictionaryCreate(
NULL, keys,
values, 1,
NULL, NULL);
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
securityError = SecPKCS12Import(inPKCS12Data,
optionsDictionary,
&items);

if (securityError == 0) {
CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);
const void *tempIdentity = NULL;
tempIdentity = CFDictionaryGetValue (myIdentityAndTrust,
kSecImportItemIdentity);
*identity = (SecIdentityRef)tempIdentity;
const void *tempTrust = NULL;
tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);
*trust = (SecTrustRef)tempTrust;
}

if (optionsDictionary) {
CFRelease(optionsDictionary);
}

return securityError;
}

我的连接失败,并出现以下错误。

{
NSErrorFailingURLKey = "https://myIpdaddress/Service1.svc/test/random";
NSErrorFailingURLStringKey = "https://myIpdaddress/Service1.svc/test/random";
NSLocalizedDescription = "The server \U201cmyIpdaddress\U201d requires a client certificate.";
NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1206 \"The server \U201cmyIpdaddress\U201d requires a client certificate.\" UserInfo=0x4b240b0 {NSErrorFailingURLKey=https://myIpdaddress/Service1.svc/test/random, NSErrorFailingURLStringKey=https://myIpdaddress/Service1.svc/test/random, NSLocalizedDescription=The server \U201cmyIpdaddress\U201d requires a client certificate.}";
}

请帮我解决这个问题。

最佳答案

我遇到了同样的问题。

我的解决方法是更正“主机”HTTP header 。

我使用的包括端口和部分路径。一旦我更正此 header 以仅包含 url 的主机部分,事情就开始工作了。

我认为当我的主机 header 错误时,服务器拒绝了我的身份,并且我认为“需要客户端证书”消息是一般响应,也可能意味着服务器不接受所提供的证书。

关于iphone - 无法在 iPhone 中使用客户端证书进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8677717/

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