Zig Requires Chilkat v11.0.0+
Zig
Examine Client Certificates for an Accepted TLS Connection
See more Socket/SSL/TLS Examples
Demonstrates how to access the client certificates for a TLS connection accepted by your application acting as the server.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const listen_ssl_socket = try chilkat.Socket.init();
defer listen_ssl_socket.deinit();
// An SSL/TLS server needs a digital certificate. This example loads it from a PFX file.
// This is the server's certificate.
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadPfxFile("qa_data/serverCert/myServerCert.pfx", "pfx_password") catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
// To accept client client certificates in the TLS handshake,
// we must indicate a list of acceptable client certificate root CA DN's
// that are allowed. (DN is an acronym for Distinguished Name.)
// Call AddSslAcceptableClientCaDn once for each acceptable CA DN.
// Here are a few examples so you can see the general format of a DN.
listen_ssl_socket.addSslAcceptableClientCaDn("C=SE, O=AddTrust AB, OU=AddTrust External TTP Network, CN=AddTrust External CA Root") catch {};
listen_ssl_socket.addSslAcceptableClientCaDn("O=Digital Signature Trust Co., CN=DST Root CA X3") catch {};
// Initialize with our server's TLS certificate.
listen_ssl_socket.initSslServer(cert) catch {
std.debug.print("{s}\n", .{try listen_ssl_socket.getLastErrorText(alloc)});
return;
};
// Bind and listen on a port:
const my_port = 8123;
// Allow for a max of 5 queued connect requests.
const back_log = 5;
listen_ssl_socket.bindAndListen(my_port, back_log) catch {
std.debug.print("{s}\n", .{try listen_ssl_socket.getLastErrorText(alloc)});
return;
};
// Accept the next incoming connection.
const max_wait_millisec = 20000;
const client_sock = try chilkat.Socket.init();
defer client_sock.deinit();
listen_ssl_socket.acceptNext(max_wait_millisec, client_sock) catch {
std.debug.print("{s}\n", .{try listen_ssl_socket.getLastErrorText(alloc)});
return;
};
// Examine the client certs chain. The 1st cert will be the client certificate, and
// the subsequent certs will be the certs in the chain of authentication.
const num_client_certs = client_sock.getNumReceivedClientCerts();
std.debug.print("numClientCerts = {d}\n", .{num_client_certs});
const client_cert = try chilkat.Cert.init();
defer client_cert.deinit();
var i: i32 = 0;
while (i < num_client_certs) {
client_sock.getRcvdClientCert(i, client_cert) catch {};
std.debug.print("{s}\n", .{try client_cert.getSubjectDN(alloc)});
i = i + 1;
}
// Close the connection with the client
try client_sock.close(1000);
}