Zig
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const socket = try chilkat.Socket.init();
defer socket.deinit();
// 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.
const pfx_password = "myPfxPassword";
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadPfxFile("qa_data/server.pfx", pfx_password) catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
socket.initSslServer(cert) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
socket.bindAndListen(5000, 25) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// Accept a client connection. The accepted connection carries any client certificate presented
// during the TLS handshake.
const connected_sock = try chilkat.Socket.init();
defer connected_sock.deinit();
socket.acceptNext(20000, connected_sock) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// Copy each received client certificate and inspect it.
const num_client_certs = connected_sock.getNumReceivedClientCerts();
const client_cert = try chilkat.Cert.init();
defer client_cert.deinit();
var i: i32 = 0;
i = 0;
while (i < num_client_certs) : (i += 1) {
connected_sock.getRcvdClientCert(i, client_cert) catch {
std.debug.print("{s}\n", .{try connected_sock.getLastErrorText(alloc)});
return;
};
std.debug.print("Client certificate subject: {s}\n", .{try client_cert.getSubjectCN(alloc)});
}
}