Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Verify SSL Server Certificate

See more Socket/SSL/TLS Examples

Demonstrates how to connect to an SSL server and verify its SSL certificate.

Chilkat Zig Downloads

Zig
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();

    const ssl = true;
    const max_wait_millisec = 20000;

    // The SSL server hostname may be an IP address, a domain name,
    // or "localhost".
    const ssl_server_host = "www.paypal.com";
    const ssl_server_port = 443;

    // 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;
    };

    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    var b_expired: bool = false;
    var b_revoked: bool = false;
    var b_signature_verified: bool = false;
    var b_trusted_root: bool = false;

    if (socket.getServerCert(cert)) {
        std.debug.print("Server Certificate:\n", .{});
        std.debug.print("Distinguished Name: {s}\n", .{try cert.getSubjectDN(alloc)});
        std.debug.print("Common Name: {s}\n", .{try cert.getSubjectCN(alloc)});
        std.debug.print("Issuer Distinguished Name: {s}\n", .{try cert.getIssuerDN(alloc)});
        std.debug.print("Issuer Common Name: {s}\n", .{try cert.getIssuerCN(alloc)});

        b_expired = cert.getExpired();
        b_revoked = cert.getRevoked();
        b_signature_verified = cert.getSignatureVerified();
        b_trusted_root = cert.getTrustedRoot();

        std.debug.print("Expired: {}\n", .{b_expired});
        std.debug.print("Revoked: {}\n", .{b_revoked});
        std.debug.print("Signature Verified: {}\n", .{b_signature_verified});
        std.debug.print("Trusted Root: {}\n", .{b_trusted_root});
    } else |_| {}

    // Close the connection with the server
    // Wait a max of 20 seconds (20000 millsec)
    try socket.close(20000);
}