Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.version = "0.1.0",
.dependencies = .{},
.fingerprint = 0xacfd1db3d29e1d11,
.minimum_zig_version = "0.16.0-dev.2261+d6b3dd25a",
.minimum_zig_version = "0.16.0-dev.2821+3edaef9e0",
.paths = .{
"readme.md",
"build.zig",
Expand Down
2 changes: 1 addition & 1 deletion demo/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.name = .demo,
.version = "0.0.0",
.fingerprint = 0xd642dfa0bb514ab0,
.minimum_zig_version = "0.16.0-dev.2261+d6b3dd25a",
.minimum_zig_version = "0.16.0-dev.2821+3edaef9e0",
.dependencies = .{
.tls = .{
.path = "../",
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn main(init: std.process.Init) !void {
var conn = try tls.client(&reader.interface, &writer.interface, .{
.host = host,
.root_ca = root_ca,
.now = try std.Io.Clock.real.now(io),
.now = std.Io.Clock.real.now(io),
.random = rng_impl.interface(),
});

Expand Down
2 changes: 1 addition & 1 deletion example/all_ciphers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn main(init: std.process.Init) !void {
defer root_ca.deinit(gpa);

const domain = if (args.len > 1) args[1] else "cloudflare.com";
const fail_count = run(io, root_ca, domain, try std.Io.Clock.real.now(io));
const fail_count = run(io, root_ca, domain, std.Io.Clock.real.now(io));
if (fail_count > 0) std.process.exit(1);
}

Expand Down
2 changes: 1 addition & 1 deletion example/badssl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn main(init: std.process.Init) !void {
cmn.get(io, domain, if (sd.port == 0) null else sd.port, false, false, .{
.root_ca = root_ca,
.host = "",
.now = try std.Io.Clock.real.now(io),
.now = std.Io.Clock.real.now(io),
.rng = rng_impl.interface(),
}) catch |err| {
std.debug.print(
Expand Down
2 changes: 1 addition & 1 deletion example/client_auth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn main(init: std.process.Init) !void {
.auth = &auth,
.diagnostic = &diagnostic,
.key_log_callback = tls.config.key_log.init(init.minimal.environ),
.now = try std.Io.Clock.real.now(io),
.now = std.Io.Clock.real.now(io),
.rng = rng_impl.interface(),
});

Expand Down
24 changes: 12 additions & 12 deletions example/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub const Counter = struct {
err,
};

mu: std.Thread.Mutex = .{},
mu: std.Io.Mutex = .init,

success: usize = 0,
fail: usize = 0,
Expand All @@ -174,9 +174,9 @@ pub const Counter = struct {
tls_1_2: usize = 0,
tls_1_3: usize = 0,

pub fn add(self: *@This(), res: Result) void {
self.mu.lock();
defer self.mu.unlock();
pub fn add(self: *@This(), io: std.Io, res: Result) void {
self.mu.lockUncancelable(io);
defer self.mu.unlock(io);

switch (res) {
.success => self.success += 1,
Expand All @@ -186,9 +186,9 @@ pub const Counter = struct {
}
}

pub fn addSuccess(self: *@This(), version: tls.config.Version) void {
self.mu.lock();
defer self.mu.unlock();
pub fn addSuccess(self: *@This(), io: std.Io, version: tls.config.Version) void {
self.mu.lockUncancelable(io);
defer self.mu.unlock(io);

self.success += 1;
switch (version) {
Expand All @@ -198,16 +198,16 @@ pub const Counter = struct {
}
}

pub fn total(self: *@This()) usize {
self.mu.lock();
defer self.mu.unlock();
pub fn total(self: *@This(), io: std.Io) usize {
self.mu.lockUncancelable(io);
defer self.mu.unlock(io);
return self.success + self.fail + self.skip + self.err;
}

pub fn show(self: *@This()) void {
pub fn show(self: *@This(), io: std.Io) void {
std.debug.print(
"stats:\n\t total: {}\n\t success: {}\n\t\t tls 1.2: {}\n\t\t tls 1.3: {}\n\t fail: {}\n\t error: {}\n\t skip: {}\n\n",
.{ self.total(), self.success, self.tls_1_2, self.tls_1_3, self.fail, self.err, self.skip },
.{ self.total(io), self.success, self.tls_1_2, self.tls_1_3, self.fail, self.err, self.skip },
);
std.debug.print("\t max client record: {d:>5}\n", .{self.max_client_record_len});
std.debug.print("\t max server record: {d:>5}\n", .{self.max_server_record_len});
Expand Down
2 changes: 1 addition & 1 deletion example/fuzz_server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn main(init: std.process.Init) !void {
var client_root_ca = try tls.config.cert.fromFilePath(gpa, io, dir, "minica.pem");
defer client_root_ca.deinit(gpa);

const now = try std.Io.Clock.real.now(io);
const now = std.Io.Clock.real.now(io);
const rng_impl: std.Random.IoSource = .{ .io = io };
const rng = rng_impl.interface();

Expand Down
2 changes: 1 addition & 1 deletion example/http_get.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn main(init: std.process.Init) !void {
// .cipher_suites = tls.config.cipher_suites.tls12,
.cipher_suites = tls.config.cipher_suites.secure,
.key_log_callback = tls.config.key_log.init(init.minimal.environ),
.now = try std.Io.Clock.real.now(io),
.now = std.Io.Clock.real.now(io),
.rng = rng_impl.interface(),
});
}
Expand Down
2 changes: 1 addition & 1 deletion example/http_get2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn main(init: std.process.Init) !void {
.host = host,
.root_ca = root_ca,
.diagnostic = &diagnostic,
.now = try std.Io.Clock.real.now(io),
.now = std.Io.Clock.real.now(io),
.rng = rng_impl.interface(),
});

Expand Down
2 changes: 1 addition & 1 deletion example/http_get_nonblock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn main(init: std.process.Init) !void {
.root_ca = ca_bundle,
.cipher_suites = tls.config.cipher_suites.secure,
.key_log_callback = tls.config.key_log.init(init.minimal.environ),
.now = try std.Io.Clock.real.now(io),
.now = std.Io.Clock.real.now(io),
.rng = rng_impl.interface(),
};
var handshake = tls.nonblock.Client.init(config);
Expand Down
2 changes: 1 addition & 1 deletion example/http_get_resumption.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
const args = try init.minimal.args.toSlice(init.arena.allocator());

const now = try std.Io.Clock.real.now(io);
const now = std.Io.Clock.real.now(io);

// Get url from args
var url: []const u8 = "https://ziglang.org";
Expand Down
14 changes: 7 additions & 7 deletions example/integration_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn connectReceive(io: Io, addr: Io.net.IpAddress, opt: tls.config.Client) !void

test "server without certificate" {
const io = testing.io;
const now = try std.Io.Clock.real.now(io);
const now = std.Io.Clock.real.now(io);
const rng_impl = std.Random.IoSource{ .io = io };
const rng = rng_impl.interface();

Expand All @@ -80,7 +80,7 @@ test "server without certificate" {
test "server with ec key key pair" {
const allocator = testing.allocator;
const io = testing.io;
const now = try std.Io.Clock.real.now(io);
const now = std.Io.Clock.real.now(io);
const rng_impl = std.Random.IoSource{ .io = io };
const rng = rng_impl.interface();

Expand Down Expand Up @@ -110,7 +110,7 @@ test "server with ec key key pair" {
test "server with ec key key pair from slices" {
const allocator = testing.allocator;
const io = testing.io;
const now = try std.Io.Clock.real.now(io);
const now = std.Io.Clock.real.now(io);
const rng_impl = std.Random.IoSource{ .io = io };
const rng = rng_impl.interface();

Expand Down Expand Up @@ -143,7 +143,7 @@ test "server with ec key key pair from slices" {
test "server with rsa key key pair" {
const allocator = testing.allocator;
const io = testing.io;
const now = try std.Io.Clock.real.now(io);
const now = std.Io.Clock.real.now(io);
const rng_impl = std.Random.IoSource{ .io = io };
const rng = rng_impl.interface();

Expand Down Expand Up @@ -173,7 +173,7 @@ test "server with rsa key key pair" {
test "server request client authentication" {
const allocator = testing.allocator;
const io = testing.io;
const now = try std.Io.Clock.real.now(io);
const now = std.Io.Clock.real.now(io);
const rng_impl = std.Random.IoSource{ .io = io };
const rng = rng_impl.interface();
const dir = try std.Io.Dir.cwd().openDir(io, "example/cert", .{});
Expand Down Expand Up @@ -225,7 +225,7 @@ test "server request client authentication" {
test "server require client authentication" {
const allocator = testing.allocator;
const io = testing.io;
const now = try std.Io.Clock.real.now(io);
const now = std.Io.Clock.real.now(io);
const rng_impl = std.Random.IoSource{ .io = io };
const rng = rng_impl.interface();
const dir = try std.Io.Dir.cwd().openDir(io, "example/cert", .{});
Expand Down Expand Up @@ -275,7 +275,7 @@ test "server require client authentication" {
test "server send key update" {
const allocator = testing.allocator;
const io = testing.io;
const now = try std.Io.Clock.real.now(io);
const now = std.Io.Clock.real.now(io);
const rng_impl = std.Random.IoSource{ .io = io };
const rng = rng_impl.interface();

Expand Down
2 changes: 1 addition & 1 deletion example/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn main(init: std.process.Init) !void {
// .root_ca = client_root_ca,
// },
.auth = &auth,
.now = try std.Io.Clock.real.now(io),
.now = std.Io.Clock.real.now(io),
.rng = rng_impl.interface(),
}) catch |err| {
std.debug.print("tls failed with {}\n", .{err});
Expand Down
16 changes: 8 additions & 8 deletions example/top_sites.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn main(init: std.process.Init) !void {
std.log.debug("all task started", .{});

var elapsed: usize = 0;
while (counter.total() < tasks) {
while (counter.total(io) < tasks) {
try io.sleep(.fromSeconds(1), .real);
elapsed += 1;
if (elapsed > 10) {
Expand All @@ -55,7 +55,7 @@ pub fn main(init: std.process.Init) !void {
try group.await(io);
}

counter.show();
counter.show(io);
if (counter.failRate() > 0.01) std.process.exit(1);
}

Expand All @@ -66,7 +66,7 @@ pub fn run(gpa: std.mem.Allocator, io: Io, domain: []const u8, root_ca: tls.conf
.host = "",
.root_ca = root_ca,
.diagnostic = &diagnostic,
.now = std.Io.Clock.real.now(io) catch unreachable,
.now = std.Io.Clock.real.now(io),
.rng = rng_impl.interface(),
};
if (cmn.inList(domain, &cmn.no_keyber)) {
Expand All @@ -81,7 +81,7 @@ pub fn run(gpa: std.mem.Allocator, io: Io, domain: []const u8, root_ca: tls.conf
error.NetworkUnreachable,
error.NameServerFailure,
=> {
counter.add(.err);
counter.add(io, .err);
if (!only_fail) {
std.debug.print("➖ {s:<25} {}\n", .{ domain, err });
}
Expand All @@ -92,7 +92,7 @@ pub fn run(gpa: std.mem.Allocator, io: Io, domain: []const u8, root_ca: tls.conf
error.ReadFailed,
error.WriteFailed,
=> {
counter.add(.skip);
counter.add(io, .skip);
if (!only_fail) {
std.debug.print("➰ {s:<25} {s}\n", .{ domain, @errorName(err) });
}
Expand All @@ -104,16 +104,16 @@ pub fn run(gpa: std.mem.Allocator, io: Io, domain: []const u8, root_ca: tls.conf
if (!only_fail) {
std.debug.print("➖ {s:<25} {} curl: {}\n", .{ domain, err, curl_err });
}
counter.add(.err);
counter.add(io, .err);
return;
};
},
}
std.debug.print("❌ {s:<25} ERROR {}\n", .{ domain, err });
counter.add(.fail);
counter.add(io, .fail);
return;
};
counter.addSuccess(diagnostic.tls_version);
counter.addSuccess(io, diagnostic.tls_version);
counter.max_server_record_len = @max(counter.max_server_record_len, diagnostic.max_server_record_len);
counter.max_server_cleartext_len = @max(counter.max_server_cleartext_len, diagnostic.max_server_cleartext_len);
counter.max_client_record_len = @max(counter.max_client_record_len, diagnostic.max_client_record_len);
Expand Down
2 changes: 1 addition & 1 deletion src/handshake_client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ test "verify google.com certificate" {

const io = testing.io;
var ca_bundle: Certificate.Bundle = .{};
try ca_bundle.rescan(testing.allocator, io, try std.Io.Clock.real.now(io));
try ca_bundle.rescan(testing.allocator, io, std.Io.Clock.real.now(io));
defer ca_bundle.deinit(testing.allocator);

h.cert = .{ .host = "google.com", .skip_verify = true, .root_ca = .{}, .now_sec = 1714846451 };
Expand Down
8 changes: 4 additions & 4 deletions src/handshake_common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,26 @@ pub const cert = struct {

pub fn fromFilePath(allocator: mem.Allocator, io: Io, dir: std.Io.Dir, path: []const u8) !Bundle {
var bundle: Bundle = .{};
try bundle.addCertsFromFilePath(allocator, io, try Io.Clock.real.now(io), dir, path);
try bundle.addCertsFromFilePath(allocator, io, Io.Clock.real.now(io), dir, path);
return bundle;
}

pub fn fromFilePathAbsolute(allocator: mem.Allocator, io: Io, path: []const u8) !Bundle {
var bundle: Bundle = .{};
try bundle.addCertsFromFilePathAbsolute(allocator, io, try Io.Clock.real.now(io), path);
try bundle.addCertsFromFilePathAbsolute(allocator, io, Io.Clock.real.now(io), path);
return bundle;
}

pub fn fromSystem(allocator: mem.Allocator, io: Io) !Bundle {
var bundle: Bundle = .{};
try bundle.rescan(allocator, io, try Io.Clock.real.now(io));
try bundle.rescan(allocator, io, Io.Clock.real.now(io));
return bundle;
}

pub fn fromSlice(allocator: mem.Allocator, io: Io, slice: []const u8) !Bundle {
const base64 = std.base64.standard.decoderWithIgnore(" \t\r\n");
const size = slice.len;
const ts = try Io.Clock.real.now(io);
const ts = Io.Clock.real.now(io);

var bundle: Bundle = .{};

Expand Down
7 changes: 3 additions & 4 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ const handshake = struct {
const Server = @import("handshake_server.zig").Handshake;
};

//TODO: io first
/// Upgrades existing stream to the tls connection by the client tls handshake.
pub inline fn clientFromStream(io: std.Io, stream: anytype, opt: config.Client) !Connection {
const input, const output = streamToRaderWriter(io, stream);
const input, const output = streamToReaderWriter(io, stream);
return try client(input, output, opt);
}

Expand All @@ -46,7 +45,7 @@ pub fn client(input: *Io.Reader, output: *Io.Writer, opt: config.Client) !Connec

/// Upgrades existing stream to the tls connection by the server side tls handshake.
pub inline fn serverFromStream(io: Io, stream: anytype, opt: config.Server) !Connection {
const input, const output = streamToRaderWriter(io, stream);
const input, const output = streamToReaderWriter(io, stream);
return try server(input, output, opt);
}

Expand All @@ -61,7 +60,7 @@ pub fn server(input: *Io.Reader, output: *Io.Writer, opt: config.Server) !Connec
}

/// With default buffer sizes
inline fn streamToRaderWriter(io: std.Io, stream: anytype) struct { *Io.Reader, *Io.Writer } {
inline fn streamToReaderWriter(io: std.Io, stream: anytype) struct { *Io.Reader, *Io.Writer } {
var input_buf: [input_buffer_len]u8 = undefined;
var output_buf: [output_buffer_len]u8 = undefined;
var reader = stream.reader(io, &input_buf);
Expand Down
Loading