- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从我的应用尝试连接的服务器收到身份验证质询,因此我实现了 connection:didReceiveAuthenticationChallenge:
方法。我需要发送 SecCertificateRef
和 SecIdentityRef
。身份正在工作,但证书需要作为 NSArray
发送,我不知道如何将 CFArrayRef
转换为 NSArray
.
这是我创建身份和证书的方法:
// Returns an array containing the certificate
- (CFArrayRef)getCertificate {
SecCertificateRef certificate = nil;
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"CertificateName" ofType:@"p12"];
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;
certificate = SecCertificateCreateWithData(nil, inPKCS12Data);
SecCertificateRef certs[1] = { certificate };
CFArrayRef array = CFArrayCreate(NULL, (const void **) certs, 1, NULL);
SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
SecTrustRef myTrust;
OSStatus status = SecTrustCreateWithCertificates(array, myPolicy, &myTrust);
if (status == noErr) {
NSLog(@"No Err creating certificate");
} else {
NSLog(@"Possible Err Creating certificate");
}
return array;
}
// Returns the identity
- (SecIdentityRef)getClientCertificate {
SecIdentityRef identityApp = nil;
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"CertificateName" ofType:@"p12"];
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;
CFStringRef password = CFSTR("password");
const void *keys[] = { kSecImportExportPassphrase };//kSecImportExportPassphrase };
const void *values[] = { password };
CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
OSStatus securityError = SecPKCS12Import(inPKCS12Data, options, &items);
CFRelease(options);
CFRelease(password);
if (securityError == errSecSuccess) {
NSLog(@"Success opening p12 certificate. Items: %ld", CFArrayGetCount(items));
CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity);
} else {
NSLog(@"Error opening Certificate.");
}
return identityApp;
}
然后在connection:didReceiveAuthenticationChallenge:
中我有:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
SecIdentityRef identity = [self getClientCertificate]; // Go get a SecIdentityRef
CFArrayRef certs = [self getCertificate]; // Get an array of certificates
// Convert the CFArrayRef to a NSArray
NSArray *myArray = (__bridge NSArray *)certs;
// Create the NSURLCredential
NSURLCredential *newCredential = [NSURLCredential credentialWithIdentity:identity certificates:certs persistence:NSURLCredentialPersistenceNone];
// Send
[challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
} else {
// Failed
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
创建 NSURLCredential
时应用程序崩溃。经过进一步检查,我得出的结论是,当我将 CFArrayRef
转换为 NSArray
时,SecCertificateRef
的数据丢失了,并且该数组包含null 导致崩溃。
如何将 SecCertificateRef
放入 NSArray
中?我是否错过了一步,或者我只是做错了?
最佳答案
我终于找到答案了。我应该使用 SecIdentityCopyCertificate(identity, &certificateRef);
而不是SecCertificateCreateWithData(nil, inPKCS12Data);
创建我的证书。
关于iphone - 为 NSURLConnection 身份验证质询创建 SecCertificateRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10238559/
我从我的应用尝试连接的服务器收到身份验证质询,因此我实现了 connection:didReceiveAuthenticationChallenge: 方法。我需要发送 SecCertificateR
我正在使用 openSSL 连接到服务器。 在获得 X509 格式的证书/链后,我想使用 Apple 的安全框架来验证证书。 为此,我需要将 X.509 结构 转换为 SecCertificateRe
我有一个证书 (SecCertificateRef),我可以检查它是否有效,我可以使用 SecCertificateCopySubjectSummary 提取“摘要”。 “摘要”到底是什么?我不明白术
如果您在 iOS 中有 SecCertificateRef 和私钥,如何创建 SecIdentityRef?我有一个从服务器传递的 X509 证书和一个关联的私钥,但现在我正在努力建立对 NSURLC
我是一名优秀的程序员,十分优秀!