I recently noticed that the std lib HTTP client returns TLS related errors when sending requests to some addresses.
I haven’t seen anyone else having this exact issue, so I’m wondering if it’s related to my machine’s setup.
const std = @import("std");
pub fn main() !void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
defer _ = gpa.deinit();
var client = std.http.Client{ .allocator = gpa.allocator() };
defer client.deinit();
const uri = try std.Uri.parse("https://medium.com");
var server_header_buffer: [1024 * 1024]u8 = undefined;
var req = try client.open(.GET, uri, .{
.server_header_buffer = &server_header_buffer,
});
defer req.deinit();
try req.send();
try req.wait();
var buf: [1024 * 1024]u8 = undefined;
const len = try req.readAll(&buf);
std.debug.print("{s}", .{buf[0..len]});
}
❯ zig version
0.14.0-dev.1924+bdd3bc056
❯ zig build run
error: TlsInitializationFailed
/home/robert/Projects/zigs/master/lib/std/crypto/aes_gcm.zig:102:17: 0x1283375 in decrypt (zig_http_tls_aes)
return error.AuthenticationFailed;
^
/home/robert/Projects/zigs/master/lib/std/crypto/tls/Client.zig:470:29: 0x121b838 in init__anon_12944 (zig_http_tls_aes)
return error.TlsBadRecordMac;
^
/home/robert/Projects/zigs/master/lib/std/http/Client.zig:1357:99: 0x11534df in connectTcp (zig_http_tls_aes)
conn.data.tls_client.* = std.crypto.tls.Client.init(stream, client.ca_bundle, host) catch return error.TlsInitializationFailed;
^
/home/robert/Projects/zigs/master/lib/std/http/Client.zig:1492:14: 0x11298a0 in connect (zig_http_tls_aes)
} orelse return client.connectTcp(host, port, protocol);
^
/home/robert/Projects/zigs/master/lib/std/http/Client.zig:1640:9: 0x111f0be in open (zig_http_tls_aes)
try client.connect(valid_uri.host.?.raw, uriPort(valid_uri, protocol), protocol);
^
/home/robert/Projects/toy/zig_http_tls_aes/src/main.zig:15:15: 0x111e033 in main (zig_http_tls_aes)
var req = try client.open(.GET, uri, .{
^
If I change the uri
to https://google.com
the error becomes:
error: TlsInitializationFailed
/home/robert/Projects/zigs/master/lib/std/crypto/tls.zig:201:9: 0x127c0f2 in toError (zig_http_tls_aes)
return switch (alert) {
^
/home/robert/Projects/zigs/master/lib/std/crypto/tls/Client.zig:252:17: 0x1217353 in init__anon_12944 (zig_http_tls_aes)
try desc.toError();
^
/home/robert/Projects/zigs/master/lib/std/http/Client.zig:1357:99: 0x11534df in connectTcp (zig_http_tls_aes)
conn.data.tls_client.* = std.crypto.tls.Client.init(stream, client.ca_bundle, host) catch return error.TlsInitializationFailed;
^
/home/robert/Projects/zigs/master/lib/std/http/Client.zig:1492:14: 0x11298a0 in connect (zig_http_tls_aes)
} orelse return client.connectTcp(host, port, protocol);
^
/home/robert/Projects/zigs/master/lib/std/http/Client.zig:1640:9: 0x111f0be in open (zig_http_tls_aes)
try client.connect(valid_uri.host.?.raw, uriPort(valid_uri, protocol), protocol);
^
/home/robert/Projects/toy/zig_http_tls_aes/src/main.zig:15:15: 0x111e033 in main (zig_http_tls_aes)
var req = try client.open(.GET, uri, .{
^
curl
, Rust and Golang equivalents work as expected.
Any clue what is going wrong? Could it be a bug in the std lib?