Objective-C
Objective-C
Get a Received Client Certificate (Server Side)
See more Socket/SSL/TLS Examples
Demonstrates Socket.GetRcvdClientCert, which copies a client certificate presented during the TLS handshake of an accepted server-side connection into a Cert object.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. Use
NumReceivedClientCerts to determine the valid index range. Client certificates are supplied only when the server requests them for mutual TLS.Chilkat Objective-C Downloads
#import <CkoSocket.h>
#import <NSString.h>
#import <CkoCert.h>
BOOL success = NO;
CkoSocket *socket = [[CkoSocket alloc] init];
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// The PFX password should come from a secure source rather than being hard-coded.
NSString *pfxPassword = @"myPfxPassword";
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadPfxFile: @"qa_data/server.pfx" password: pfxPassword];
if (success == NO) {
NSLog(@"%@",cert.LastErrorText);
return;
}
success = [socket InitSslServer: cert];
if (success == NO) {
NSLog(@"%@",socket.LastErrorText);
return;
}
success = [socket BindAndListen: [NSNumber numberWithInt: 5000] backlog: [NSNumber numberWithInt: 25]];
if (success == NO) {
NSLog(@"%@",socket.LastErrorText);
return;
}
// Accept a client connection. The accepted connection carries any client certificate presented
// during the TLS handshake.
CkoSocket *connectedSock = [[CkoSocket alloc] init];
success = [socket AcceptNext: [NSNumber numberWithInt: 20000] socket: connectedSock];
if (success == NO) {
NSLog(@"%@",socket.LastErrorText);
return;
}
// Copy each received client certificate and inspect it.
int numClientCerts = [connectedSock.NumReceivedClientCerts intValue];
CkoCert *clientCert = [[CkoCert alloc] init];
int i;
for (i = 0; i <= numClientCerts - 1; i++) {
success = [connectedSock GetRcvdClientCert: [NSNumber numberWithInt: i] cert: clientCert];
if (success == NO) {
NSLog(@"%@",connectedSock.LastErrorText);
return;
}
NSLog(@"%@%@",@"Client certificate subject: ",clientCert.SubjectCN);
}