-
Notifications
You must be signed in to change notification settings - Fork 10
/
stdssl.zig
43 lines (34 loc) · 1.26 KB
/
stdssl.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const std = @import("std");
pub fn init() anyerror!void {
}
pub const SslConn = struct {
pub const Pinned = struct { };
stream: std.net.Stream,
client: std.crypto.tls.Client,
pub fn init(stream: std.net.Stream, serverName: []const u8, pinned: *Pinned) !SslConn {
_ = pinned;
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
var ca_bundle = std.crypto.Certificate.Bundle{ };
defer ca_bundle.deinit(arena.allocator());
try ca_bundle.rescan(arena.allocator());
return SslConn {
.stream = stream,
.client = try std.crypto.tls.Client.init(stream, ca_bundle, serverName),
};
}
pub fn deinit(self: *SslConn) void {
// TODO: do I need to close the stream?
self.* = undefined;
}
pub fn read(self: *SslConn, data: []u8) !usize {
const len = try self.client.read(self.stream, data);
//std.log.info("stdssl: read {} bytes", .{len});
return len;
}
pub fn write(self: *SslConn, data: []const u8) !usize {
const written = try self.client.write(self.stream, data);
//std.log.info("stdssl: write {} bytes, wrote {}", .{data.len, written});
return written;
}
};