OpenSSL"服务器' localhost'需要客户端证书。":iOS 8与localhost apache服务器

时间:2015-07-24 10:01:25

标签: ios openssl xampp nsurlconnection server-configuration

我正面临OpenSSL证书创建和安装相关的问题,或者可能是由于NSURLConnection,我已经创建了OpenSSL证书 following this

这是错误的localizedDescription

2015-07-24 14:47:32.279 SSLTest[7657:60489] CFNetwork SSLHandshake failed (-9824 -> -9829)
2015-07-24 14:47:32.347 SSLTest[7657:60489] NSURLConnection/CFURLConnection       
HTTP load failed (kCFStreamErrorDomainSSL, -9829)
2015-07-24 14:47:32.576 SSLTest[7657:60350] The server “localhost” requires a client certificate.

每次我得到"服务器' localhost'需要客户证书"错误。

这是我的.m

- (void)viewDidLoad
{
   [super viewDidLoad];

   NSString *url = @"https://localhost/Test.php";

   NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
   [theRequest setHTTPMethod:@"POST"];

   NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
   [theConnection start];
}

#pragma mark - NSURLConnectionDelegate

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{    
    if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
       [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
    } else if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodClientCertificate) {

       SecIdentityRef identity = [self getClientCertificate];
       SecCertificateRef certificateRef;
       SecIdentityCopyCertificate(identity, &certificateRef);
       CFArrayRef certificateArray = CFArrayCreate(NULL, (const void **)certificateRef, 1, NULL);

       NSURLCredential *identityCredential = [NSURLCredential credentialWithIdentity:identity certificates:(__bridge NSArray *)certificateArray persistence:NSURLCredentialPersistenceForSession];
       [challenge.sender useCredential:identityCredential forAuthenticationChallenge:challenge];
   }
 }

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
   NSLog(@"%@",[error localizedDescription]);
}

#pragma mark - Certificates

- (SecIdentityRef)getClientCertificate {
    SecIdentityRef identityApp = nil;
    NSString *thePath = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"];
    NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
    CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;
    CFStringRef password = CFSTR("abcXYZ123");
    const void *keys[] = { 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;
}

我的httpd.config看起来像这样(我的本地mac-mini系统上有BITNAMI XAMPP 5.6.8)

Alias /bitnami/ "/Applications/XAMPP/xamppfiles/apache2/htdocs/"
Alias /bitnami "/Applications/XAMPP/xamppfiles/apache2/htdocs"


SSLEngine on
SSLCertificateFile /Applications/XAMPP/xamppfiles/apache2/htdocs/ca.crt
SSLCertificateKeyFile /Applications/XAMPP/xamppfiles/apache2/htdocs/ca_withoutPassword.key

SSLCACertificateFile /Applications/XAMPP/xamppfiles/apache2/htdocs/ca.crt

SSLVerifyClient require

<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs">
   Options Indexes FollowSymLinks
   AllowOverride All
   Order allow,deny
   Allow from all
</Directory>

我曾尝试使用SSLCertificateChainFile和SSLCACertificateFile,但没有运气。

如果我删除了&#39; SSLVerifyClient需要&#39; 并点击服务,那么我会正确接收authenticationMethod &#39; NSURLAuthenticationMethodServerTrust&#39; 和服务器响应。

如果我在.config文件中添加&#39; SSLVerifyClient require&#39; ,则NSURLConnection委托方法&#39; willSendRequestForAuthenticationChallenge&#39; 将调用两次,一次使用&#39; NSURLAuthenticationMethodServerTrust&#39; ,第二次使用&#39; NSURLAuthenticationMethodClientCertificate&#39; ,然后我将收到以上错误。

1 个答案:

答案 0 :(得分:1)

我认为你的Apache设置错了;您将SSLCACertificateFile指向您的客户端证书,而客户端证书位于客户端;您应该将其指向CA证书。这是用于验证客户端证书的内容。

作为旁注,上面的配置真的不安全。切勿在公共htdocs文件夹上放置任何SSL证书。它们应放在只能由root读取的地方,肯定远程用户无法访问。你以这种方式暴露自己最严重的攻击。