How to use OpenSSL-Zig library

Hello, so I’ve been creating a small networking project to brush up more on networking. I’ve been having a blast with it so far but recently I came across an idea. I was thinking of using OpenSSL, to practice encrypting the session and came across this project called openssl-zig. I’ve never used OpenSSL before let alone had to import another project into my already existing Zig project. I tried to follow this guide for some help but can’t seem to get it working. I’m thinking maybe it’s because there is no root.zig or main.zig files for me to properly configure my project’s build.zig file. I put off this project for a while now because I got stuck with this situation, thinking I could do the encryption manually (I couldn’t lol) but it’s gotten to the point to where I might as well use a library for some help. Any advice or guides would be much appreciated, thank you again as always :smiley: .

P.S. I hope you guys are having a fun spooky month.

The guide is about zig source modules.

The openssl-zig exposes the openssl package with two static library artifacts: crypto and ssl. The package build also installs the headers of crypto and openssl under the include directory.
You need to add openssl package as a dependency in your build.zig.zon, for example using zig fetch --save git+https://github.com/kassane/openssl-zig#HEAD.
Then, in your build.zig you need to add the artifacts of the dependency as libraries and add the include directory to your include path using: exe.addIncludePath(b.path("include"));

There is an easier way to use openssl: to install a package of openssl for your system and the add in your build.zig:

    exe.linkSystemLibrary("openssl");

Zig is using pkg-config to find the link and compile flags for the library.

For both ways you need to add:

const openssl = @cImport({
    @cInclude("openssl/ssl.h");
});

One way to initialize the library is: _ = openssl.SSL_library_init();

Hi @NEETdemon,

I’m the author of the mentioned library.

If you look (assuming you have used build.zig before) at the script it will output three artifacts/libraries that can be used standalone or called by zon (b.dependency).

The idea behind this project is to make it easier to build the openssl libraries only. There are no Zig bindings, so you need to use the headers.

const openssl_dep = b.dependency("openssl", .{
        .target = target,
        .optimize = optimize,
    });
    const libcrypto = openssl_dep.artifact("crypto");
    const libssl = openssl_dep.artifact("ssl");

    for(libcrypto.root_module.include_dirs.items) |include_dir| {
        try exe.root_module.include_dirs.append(b.allocator, include_dir);
    }
  
    exe.linkLibrary(libssl);
    exe.linkLibrary(libcrypto);