Ios 如何在RESTfulWeb服务中使用带有相互认证的DER编码证书?

Ios 如何在RESTfulWeb服务中使用带有相互认证的DER编码证书?,ios,https,openssl,ssl-certificate,mutual-authentication,Ios,Https,Openssl,Ssl Certificate,Mutual Authentication,目前,我正在开发一个应用程序,它使用相互身份验证与REST接口通信。因为我对这个话题很陌生,所以我研究了几个例子——现在我有一些问题。我希望我能把所有的知识拼凑在一起,以便更好地了解整个过程 该过程应如下所示: 我获得了导出的服务器证书,其中包含服务器的公钥,并将其添加到应用程序包中(稍后用于SSL固定) 第一个请求表示双向身份验证的开始,它是一个Post请求,其中还包含一个证书签名请求,以获取客户端证书(与REST接口的安全部分进行通信时需要该证书)。CSR是通过openSSL库在代码中动态生

目前,我正在开发一个应用程序,它使用相互身份验证与REST接口通信。因为我对这个话题很陌生,所以我研究了几个例子——现在我有一些问题。我希望我能把所有的知识拼凑在一起,以便更好地了解整个过程

该过程应如下所示:

  • 我获得了导出的服务器证书,其中包含服务器的公钥,并将其添加到应用程序包中(稍后用于SSL固定)
  • 第一个请求表示双向身份验证的开始,它是一个Post请求,其中还包含一个证书签名请求,以获取客户端证书(与REST接口的安全部分进行通信时需要该证书)。CSR是通过openSSL库在代码中动态生成的
  • 服务器的响应返回用于客户端身份验证的已签名证书。此证书采用DER格式,并采用Base64编码 我用于为第一个请求执行SSL固定的代码如下所示,SSL固定工作正常

    - (void)connection:(NSURLConnection *)connection
    willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"server-cert" ofType:@"cer"];
        SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
        SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, 0);
    
        NSData *remoteCertificateData =   CFBridgingRelease(SecCertificateCopyData(certificate));
        NSData *localCertificateData = [NSData dataWithContentsOfFile:cerPath];
    
    
        if ([remoteCertificateData isEqualToData:localCertificateData]) {
    
            NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
            [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    
            [[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential forProtectionSpace:[challenge protectionSpace]];
    
            NSLog(@"Certificate Pinning Succeeded");
    
        } else {
    
            [[challenge sender] cancelAuthenticationChallenge:challenge];
    
            NSLog(@"Certificate Pinning Failed");
    
        }
    }
    
    但是如何处理从服务器返回的证书?据我所知,我必须使用以下NSURLConnection委托方法,并以某种方式将此证书提供给服务器(用于进一步请求)

    现在是我的问题。我看到了几个使用PKCS12格式(需要私钥和证书颁发机构)而不是DER编码的证书来对服务器进行自我身份验证的示例。但是,对于进一步的请求,如何使用“didReceiveAuthenticationChallenge”中DER格式的签名证书?还有一个Android应用程序使用相同的过程进行相互身份验证,它不需要PKCS12证书

    我很乐意得到一些提示。Thx.

    相关,请参阅和。两者都依赖于一个长期的客户端私钥来避免MitM。与传统的相互身份验证不同,客户端证书和私钥不需要由组织颁发。另请参见和。
    - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:   (NSURLProtectionSpace *)protectionSpace
    {
        if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust]) {
    
            NSLog(@"Wants to Authenticate Server Trust");
    
            return YES;
        }
    
        if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodClientCertificate]) {
    
            NSLog(@"Wants to Authenticate Client Certificate");
    
            return YES;
        }
    
        return NO;
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge