3

canAuthenticateAgainstProtectionSpace(のデリゲートコールバックNSURLConnection)の既知の値に対して公開鍵をチェックするように求められました

これは私がこれまでに持っているものです:

- (BOOL)connection:(NSURLConnection *)connection 
        canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
    {
        SecKeyRef publicKey = SecTrustCopyPublicKey([protectionSpace serverTrust]);

        NSLog(@"%@",SecTrustCopyPublicKey([protectionSpace serverTrust])); 
        return YES;
}

公開鍵を既知の値と比較するにはどうすればよいですか?

NSLogは以下を生成します:<SecKeyRef: 0x687c000>これは有用な変化ではありません。

4

2 に答える 2

5

誰かが気にかけている場合、解決策は、バンドルに保存されている証明書を使用して、証明書のバイトをチェックすることでした。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
    SecTrustRef trust = [protectionSpace serverTrust];

    SecCertificateRef certificate = SecTrustGetCertificateAtIndex(trust, 0);

    NSData* ServerCertificateData = (NSData*) SecCertificateCopyData(certificate);

    // Check if the certificate returned from the server is identical to the saved certificate in
    // the main bundle
    BOOL areCertificatesEqual = ([ServerCertificateData 
                                  isEqualToData:[MyClass getCertificate]]);

    [ServerCertificateData release];

    if (!areCertificatesEqual) 
    {    
        NSLog(@"Bad Certificate, canceling request");
        [connection cancel];
    }

    // If the certificates are not equal we should not talk to the server;
    return areCertificatesEqual;
}
于 2011-09-09T13:05:48.137 に答える
4

SecCertificateCopyDataは、証明書を「DER」形式のDistinguishedEncodingRulesで返すことに注意してください。そのため、証明書をpemやその他の形式ではなく、その形式でアプリに組み込む必要があります。opensslを使用して証明書をDERに変換するには、次のコマンドを使用します。openssl x509 -in server.crt -out server.der -outform DER

于 2012-09-14T16:38:39.617 に答える