Objective-C
Objective-C
Get the Server Certificate from a TLS Connection
See more Socket/SSL/TLS Examples
Demonstrates Socket.GetServerCert, which copies the certificate presented by the remote TLS server into a Cert object for inspection.
Background. The method returns false when the socket is not connected, is not using TLS, or no server certificate is available. Inspecting the certificate lets an application validate the server's identity.
Chilkat Objective-C Downloads
#import <CkoSocket.h>
#import <CkoCert.h>
BOOL success = NO;
CkoSocket *socket = [[CkoSocket alloc] init];
// Connect to the server using TLS.
BOOL bTls = YES;
int maxWaitMs = 5000;
success = [socket Connect: @"example.com" port: [NSNumber numberWithInt: 5000] ssl: bTls maxWaitMs: [NSNumber numberWithInt: maxWaitMs]];
if (success == NO) {
NSLog(@"%@",socket.LastErrorText);
return;
}
// Copy the certificate presented by the remote TLS server, then inspect it. This can be used to
// validate the server's identity.
CkoCert *cert = [[CkoCert alloc] init];
success = [socket GetServerCert: cert];
if (success == NO) {
NSLog(@"%@",socket.LastErrorText);
return;
}
NSLog(@"%@%@",@"Server certificate subject: ",cert.SubjectCN);
NSLog(@"%@%@",@"Issued by: ",cert.IssuerCN);