4

I made a simple TLS Server with GCDAsyncSocket and want to get the clients public key. I tried using this:

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    SSLContextRef ref = [sock sslContext];
    SecTrustRef trust;
    SSLCopyPeerTrust(ref, &trust);

    SecKeyRef key = SecTrustCopyPublicKey(trust);
    NSLog(@"%@",key);
}

but i get a exc_bad_access in the SecTrustCopyPublicKey method. How can i get the public key as a NSString/NSData?

thomasguenzel
  • 670
  • 7
  • 25

1 Answers1

0
  1. Are you sure there's an SSL connection? Does your didSecure delegate method invoked?
  2. Check your trust reference is not NULL after the SSLCopyPeerTrust function performed.
  3. There's a discussion about the SecTrustCopyPublicKey on the Certificate, Key, and Trust Services Reference provided by the Apple.

You must call the SecTrustEvaluate function before calling this function. When you call this function, it attempts to return the public key of the leaf certificate, even if the trust evaluation was unsuccessful. Even if the trust evaluation was successful, this function might still return NULL—for example, if the leaf certificate’s key can’t be extracted for some reason.

Daniyar
  • 2,975
  • 2
  • 26
  • 39