Zig Requires Chilkat v10.1.2+
Zig
SSL Client Certificate
See more Socket/SSL/TLS Examples
Demonstrates how to connect to an SSL server using a client-side certificate, send a simple message, receive a simple response, and disconnect.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 socket = try chilkat.Socket.init();
defer socket.deinit();
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
const cert_store = try chilkat.CertStore.init();
defer cert_store.deinit();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
cert_store.loadPfxFile("chilkat_secret.pfx", "secret") catch {
std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
return;
};
// Find the certificate by the subject common name:
const json_cn = try chilkat.JsonObject.init();
defer json_cn.deinit();
json_cn.updateString("CN", "cert common name") catch {};
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert_store.findCert(json_cn, cert) catch {
std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
return;
};
try socket.setSslClientCert(cert);
const ssl = true;
const max_wait_millisec = 20000;
// The SSL server hostname may be an IP address, a domain name,
// or "localhost". You'll need to change this:
const ssl_server_host = "123.123.88.88";
const ssl_server_port = 8123;
// Connect to the SSL server:
socket.connect(ssl_server_host, ssl_server_port, ssl, max_wait_millisec) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// Set maximum timeouts for reading an writing (in millisec)
socket.setMaxReadIdleMs(20000);
socket.setMaxSendIdleMs(20000);
// Send a "Hello Server! -EOM-" message:
socket.sendString("Hello Server! -EOM-") catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// The server (in this example) is going to send a "Hello Client! -EOM-"
// message. Read it:
const received_msg = socket.receiveUntilMatch(alloc, "-EOM-") catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// Close the connection with the server
// Wait a max of 20 seconds (20000 millsec)
try socket.close(20000);
std.debug.print("{s}\n", .{received_msg});
}