iOS and SSL: Unable to validate self-signed server certificate

前端 未结 2 1837
执笔经年
执笔经年 2020-12-20 02:34


    I\'m fairly new to consuming webservices using SSL channel. After fairly good search I had found a way to perform SSL/HTTPS authentication using

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 03:16

    I did figure out how to resolve this issue.

    I ended up comparing the client and server trust certificates, byte-by-byte. Although there could be another way to resolve such issues of self-signed certificate, but for this solution did work. Here is how I'm doing comparison of the client and server certificates, byte-by-byte, using their CFData objects(you can also reference 'AdvancedURLConnections' example code provided by Apple):

    success = NO;
            pServerCert = SecTrustGetLeafCertificate(trust);
            if (clientCert != NULL) {
                CFDataRef       clientCertData;
                CFDataRef       serverCertData;
    
                clientCertData = SecCertificateCopyData(clientCert);
                serverCertData   = SecCertificateCopyData(pServerCert);
    
                assert(clientCertData != NULL);
                assert(serverCertData   != NULL);
    
                success = CFEqual(clientCertData, serverCertData);
    
                CFRelease(clientCertData);
                CFRelease(serverCertData);
            }
            if (success) {
                [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
                [self printLogToConsole:@"Success! Trust validation successful."];
            } else {
                [self printLogToConsole:@"Failed! Trust evaluation failed for service root certificate.\n"];
                [[challenge sender] cancelAuthenticationChallenge:challenge];
            }
    

    Hope this will help someone, who is looking for solution of similar issue,

    Thanks.

提交回复
热议问题