Skip to content

Commit 732a0c7

Browse files
committed
feat(tls): switch from bearssl to secsock
1 parent 550f44b commit 732a0c7

File tree

22 files changed

+138
-1016
lines changed

22 files changed

+138
-1016
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44

55
## Installing
6-
Latest Zig Stable: `0.13.0`
6+
Compatible Zig Version: `0.13.0`
77

8-
Latest zzz release: `0.2.0`
8+
Compatible [tardy](https://github.com/tardy-org/tardy) Version: `95239a7d9547161869f365bd6cd3f0255b37c49b`
9+
10+
Latest Release: `0.2.0`
911
```
1012
zig fetch --save git+https://github.com/tardy-org/zzz#v0.2.0
1113
```
@@ -21,7 +23,7 @@ exe.root_module.addImport(zzz);
2123
```
2224

2325
## zzz?
24-
zzz is a framework for writing performant and reliable networked services in Zig. It supports both HTTP and HTTPS (using BearSSL for TLS).
26+
zzz is a framework for writing performant and reliable networked services in Zig. It supports both HTTP and HTTPS.
2527

2628
zzz currently supports Linux, Mac and Windows. Linux is currently the recommended target for deployments.
2729

@@ -60,7 +62,7 @@ zzz can be configured to utilize minimal memory while remaining performant. The
6062
- `poll` for Linux, Mac and Windows.
6163
- Layered Router, including Middleware
6264
- Single and Multithreaded Support
63-
- TLS using BearSSL
65+
- TLS using [secsock](https://github.com/tardy-org/secsock)
6466
- Memory Pooling for minimal allocations
6567

6668
## Contribution

build.zig

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ pub fn build(b: *std.Build) void {
1717

1818
zzz.addImport("tardy", tardy);
1919

20-
const bearssl = b.dependency("bearssl", .{
20+
const secsock = b.dependency("secsock", .{
2121
.target = target,
2222
.optimize = optimize,
23-
// Without this, you get an illegal instruction error on certain paths.
24-
// This makes it slightly slower but prevents faults.
25-
.BR_LE_UNALIGNED = false,
26-
.BR_BE_UNALIGNED = false,
27-
}).artifact("bearssl");
23+
}).module("secsock");
2824

29-
zzz.linkLibrary(bearssl);
25+
zzz.addImport("secsock", secsock);
3026

3127
add_example(b, "basic", false, target, optimize, zzz);
3228
add_example(b, "cookies", false, target, optimize, zzz);
@@ -45,7 +41,7 @@ pub fn build(b: *std.Build) void {
4541
.root_source_file = b.path("./src/tests.zig"),
4642
});
4743
tests.root_module.addImport("tardy", tardy);
48-
tests.root_module.linkLibrary(bearssl);
44+
tests.root_module.addImport("secsock", secsock);
4945

5046
const run_test = b.addRunArtifact(tests);
5147
run_test.step.dependOn(&tests.step);

build.zig.zon

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
.minimum_zig_version = "0.13.0",
55
.dependencies = .{
66
.tardy = .{
7-
.url = "git+https://github.com/mookums/tardy#084f5e04b333a68c6b3c433de241f3dcc368b84b",
8-
.hash = "1220960cc794c77b2b1fcab1de6fcf37c6c9571433473541007a0a9258dd7f4f8c0b",
7+
.url = "git+https://github.com/tardy-org/tardy#95239a7d9547161869f365bd6cd3f0255b37c49b",
8+
.hash = "1220dfee39cf3cdb58b3d5f3001078297f02997b617990da84bdfdc506b46ece56ab",
99
},
10-
.bearssl = .{
11-
.url = "git+https://github.com/mookums/bearssl-zig#37a96eee56fe2543579bbc6da148ca886f3dd32b",
12-
.hash = "12200e89d16612100a2f145cfa292537ac25b2205735fc1c644c799d2995f94e8e20",
10+
.secsock = .{
11+
.url = "git+https://github.com/tardy-org/secsock#b532b29e046c0a05d94a76f8c135c80679e89e54",
12+
.hash = "1220264235961dbd2a0240567951be585e0087c3c69c79ebbbc37180f4ae8b15a750",
1313
},
1414
},
1515

docs/https.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# HTTPS
2-
zzz utilizes [BearSSL](https://bearssl.org/) to provide a safe and performant TLS implementation. This TLS functionality is entirely separated from the I/O for maximum portability.
2+
zzz utilizes [secsock](https://github.com/tardy-org/secsock) to provide a safe and performant TLS implementation. This TLS functionality is entirely separated from the I/O for maximum portability.
33

44
*Note: TLS Support is not **entirely** complete yet. It's a very rough area that will be getting cleaned up in a future development cycle*
55

@@ -23,7 +23,11 @@ const Route = http.Route;
2323
const Router = http.Router;
2424
const Respond = http.Respond;
2525
26-
fn root_handler(_: *const Context, _: void) !Respond {
26+
const secsock = zzz.secsock;
27+
const SecureSocket = secsock.SecureSocket;
28+
const Compression = http.Middlewares.Compression;
29+
30+
fn root_handler(ctx: *const Context, _: void) !Respond {
2731
const body =
2832
\\ <!DOCTYPE html>
2933
\\ <html>
@@ -36,11 +40,11 @@ fn root_handler(_: *const Context, _: void) !Respond {
3640
\\ </html>
3741
;
3842
39-
return Respond{ .standard = .{
43+
return ctx.response.apply(.{
4044
.status = .OK,
4145
.mime = http.Mime.HTML,
4246
.body = body[0..],
43-
} };
47+
});
4448
}
4549
4650
pub fn main() !void {
@@ -53,11 +57,12 @@ pub fn main() !void {
5357
const allocator = gpa.allocator();
5458
defer _ = gpa.deinit();
5559
56-
var t = try Tardy.init(allocator, .{ .threading = .single });
60+
var t = try Tardy.init(allocator, .{ .threading = .auto });
5761
defer t.deinit();
5862
5963
var router = try Router.init(allocator, &.{
6064
Route.init("/").get({}, root_handler).layer(),
65+
Compression(.{ .gzip = .{} }),
6166
Route.init("/embed/pico.min.css").embed_file(
6267
.{ .mime = http.Mime.CSS },
6368
@embedFile("embed/pico.min.css"),
@@ -71,25 +76,22 @@ pub fn main() !void {
7176
try socket.bind();
7277
try socket.listen(1024);
7378
79+
var s2n = try secsock.s2n.init(allocator);
80+
defer s2n.deinit();
81+
try s2n.add_cert_chain(@embedFile("certs/cert.pem"), @embedFile("certs/key.pem"));
82+
const secure = try s2n.to_secure_socket(socket, .server);
83+
7484
const EntryParams = struct {
7585
router: *const Router,
76-
socket: Socket,
86+
socket: SecureSocket,
7787
};
7888
7989
try t.entry(
80-
EntryParams{ .router = &router, .socket = socket },
90+
EntryParams{ .router = &router, .socket = secure },
8191
struct {
8292
fn entry(rt: *Runtime, p: EntryParams) !void {
83-
var server = Server.init(rt.allocator, .{
84-
.security = .{ .tls = .{
85-
.cert = .{ .file = .{ .path = "./examples/tls/certs/cert.pem" } },
86-
.key = .{ .file = .{ .path = "./examples/tls/certs/key.pem" } },
87-
.cert_name = "CERTIFICATE",
88-
.key_name = "EC PRIVATE KEY",
89-
} },
90-
.stack_size = 1024 * 1024 * 8,
91-
});
92-
try server.serve(rt, p.router, p.socket);
93+
var server = Server.init(.{ .stack_size = 1024 * 1024 * 8 });
94+
try server.serve(rt, p.router, .{ .secure = p.socket });
9395
}
9496
}.entry,
9597
);

examples/basic/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ pub fn main() !void {
5454
EntryParams{ .router = &router, .socket = socket },
5555
struct {
5656
fn entry(rt: *Runtime, p: EntryParams) !void {
57-
var server = Server.init(rt.allocator, .{
57+
var server = Server.init(.{
5858
.stack_size = 1024 * 1024 * 4,
5959
.socket_buffer_bytes = 1024 * 2,
6060
.keepalive_count_max = null,
6161
.connection_count_max = 1024,
6262
});
63-
try server.serve(rt, p.router, p.socket);
63+
try server.serve(rt, p.router, .{ .normal = p.socket });
6464
}
6565
}.entry,
6666
);

examples/cookies/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ pub fn main() !void {
6363
EntryParams{ .router = &router, .socket = socket },
6464
struct {
6565
fn entry(rt: *Runtime, p: EntryParams) !void {
66-
var server = Server.init(rt.allocator, .{
66+
var server = Server.init(.{
6767
.stack_size = 1024 * 1024 * 4,
6868
.socket_buffer_bytes = 1024 * 2,
6969
.keepalive_count_max = null,
7070
.connection_count_max = 10,
7171
});
72-
try server.serve(rt, p.router, p.socket);
72+
try server.serve(rt, p.router, .{ .normal = p.socket });
7373
}
7474
}.entry,
7575
);

examples/form/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ pub fn main() !void {
107107
EntryParams{ .router = &router, .socket = socket },
108108
struct {
109109
fn entry(rt: *Runtime, p: EntryParams) !void {
110-
var server = Server.init(rt.allocator, .{
110+
var server = Server.init(.{
111111
.stack_size = 1024 * 1024 * 4,
112112
.socket_buffer_bytes = 1024 * 2,
113113
});
114-
try server.serve(rt, p.router, p.socket);
114+
try server.serve(rt, p.router, .{ .normal = p.socket });
115115
}
116116
}.entry,
117117
);

examples/fs/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ pub fn main() !void {
7272
EntryParams{ .router = &router, .socket = socket },
7373
struct {
7474
fn entry(rt: *Runtime, p: EntryParams) !void {
75-
var server = Server.init(rt.allocator, .{
75+
var server = Server.init(.{
7676
.stack_size = 1024 * 1024 * 4,
7777
.socket_buffer_bytes = 1024 * 4,
7878
});
79-
try server.serve(rt, p.router, p.socket);
79+
try server.serve(rt, p.router, .{ .normal = p.socket });
8080
}
8181
}.entry,
8282
);

examples/middleware/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ pub fn main() !void {
9191
EntryParams{ .router = &router, .socket = socket },
9292
struct {
9393
fn entry(rt: *Runtime, p: EntryParams) !void {
94-
var server = Server.init(rt.allocator, .{});
95-
try server.serve(rt, p.router, p.socket);
94+
var server = Server.init(.{});
95+
try server.serve(rt, p.router, .{ .normal = p.socket });
9696
}
9797
}.entry,
9898
);

examples/sse/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ pub fn main() !void {
5959
EntryParams{ .router = &router, .socket = socket },
6060
struct {
6161
fn entry(rt: *Runtime, p: EntryParams) !void {
62-
var server = Server.init(rt.allocator, .{
62+
var server = Server.init(.{
6363
.stack_size = 1024 * 1024 * 4,
6464
.socket_buffer_bytes = 1024 * 2,
6565
});
66-
try server.serve(rt, p.router, p.socket);
66+
try server.serve(rt, p.router, .{ .normal = p.socket });
6767
}
6868
}.entry,
6969
);

examples/tls/certs/cert.pem

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
-----BEGIN CERTIFICATE-----
2-
MIIB3jCCAYWgAwIBAgIUT7UpeJjQtiGQzZA3W5QVZKsDjY8wCgYIKoZIzj0EAwIw
3-
RTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGElu
4-
dGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNDA4MjgyMjMxNTlaFw0yNTA4Mjgy
5-
MjMxNTlaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYD
6-
VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjO
7-
PQMBBwNCAATApBrE0OEPXnewxHNNpRv8a9rn1TqKg5V9RTFA3OjoTaiZZqXY8Z1w
8-
rOy7qcf+cv9resH/fSoynZavYdWVbijro1MwUTAdBgNVHQ4EFgQUjhV+PXvRK+pb
9-
vQ82AQiqFCFE+VgwHwYDVR0jBBgwFoAUjhV+PXvRK+pbvQ82AQiqFCFE+VgwDwYD
10-
VR0TAQH/BAUwAwEB/zAKBggqhkjOPQQDAgNHADBEAiAg74BJcIbrHB0jQE7Usy/Y
11-
8rCKuXPL34uOnb0dTTpiJAIgY8OcnoPI493cubMOmfneEgSOU0p73BcjhZCQntZG
12-
GEE=
2+
MIIBmDCCAT+gAwIBAgIUXq+kgTxiu8vfVGXGnXmoXfZYeBwwCgYIKoZIzj0EAwIw
3+
FDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI1MDMwOTAzMTkzNVoXDTI2MDMwOTAz
4+
MTkzNVowFDESMBAGA1UEAwwJbG9jYWxob3N0MFkwEwYHKoZIzj0CAQYIKoZIzj0D
5+
AQcDQgAExBVte4Wu3SsBhfD+3uvXE5u3hExgKZGryIAXu1BgVPPuQQDcObS6QwWx
6+
+wJHVD9P/SZYjmSHKtwh7/7tn11QI6NvMG0wHQYDVR0OBBYEFC81CrnuWJdxpV9e
7+
J0aneKk2SGB4MB8GA1UdIwQYMBaAFC81CrnuWJdxpV9eJ0aneKk2SGB4MA8GA1Ud
8+
EwEB/wQFMAMBAf8wGgYDVR0RBBMwEYIJbG9jYWxob3N0hwR/AAABMAoGCCqGSM49
9+
BAMCA0cAMEQCIGq9siIGaIfclJRYjsjfGpheWeVV8XZhrIFvQ9EaZz36AiAI/Wen
10+
178H1CbdcwjpkENgfejbOdZv/E5O2aNVJwt/2A==
1311
-----END CERTIFICATE-----

examples/tls/certs/key.pem

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
-----BEGIN EC PARAMETERS-----
2-
BggqhkjOPQMBBw==
3-
-----END EC PARAMETERS-----
41
-----BEGIN EC PRIVATE KEY-----
5-
MHcCAQEEINz6Y85CkixUT1YX1g5mQs69SL1h8o9jhj9uzvD5F+SBoAoGCCqGSM49
6-
AwEHoUQDQgAEwKQaxNDhD153sMRzTaUb/Gva59U6ioOVfUUxQNzo6E2omWal2PGd
7-
cKzsu6nH/nL/a3rB/30qMp2Wr2HVlW4o6w==
2+
MHcCAQEEILClMa6ufhTsG8tGqw4N7MkjkkXthPVVfwYObQMbAvyKoAoGCCqGSM49
3+
AwEHoUQDQgAExBVte4Wu3SsBhfD+3uvXE5u3hExgKZGryIAXu1BgVPPuQQDcObS6
4+
QwWx+wJHVD9P/SZYjmSHKtwh7/7tn11QIw==
85
-----END EC PRIVATE KEY-----

examples/tls/main.zig

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const Route = http.Route;
1515
const Router = http.Router;
1616
const Respond = http.Respond;
1717

18+
const secsock = zzz.secsock;
19+
const SecureSocket = secsock.SecureSocket;
1820
const Compression = http.Middlewares.Compression;
1921

2022
fn root_handler(ctx: *const Context, _: void) !Respond {
@@ -47,7 +49,7 @@ pub fn main() !void {
4749
const allocator = gpa.allocator();
4850
defer _ = gpa.deinit();
4951

50-
var t = try Tardy.init(allocator, .{ .threading = .single });
52+
var t = try Tardy.init(allocator, .{ .threading = .auto });
5153
defer t.deinit();
5254

5355
var router = try Router.init(allocator, &.{
@@ -66,25 +68,22 @@ pub fn main() !void {
6668
try socket.bind();
6769
try socket.listen(1024);
6870

71+
var s2n = try secsock.s2n.init(allocator);
72+
defer s2n.deinit();
73+
try s2n.add_cert_chain(@embedFile("certs/cert.pem"), @embedFile("certs/key.pem"));
74+
const secure = try s2n.to_secure_socket(socket, .server);
75+
6976
const EntryParams = struct {
7077
router: *const Router,
71-
socket: Socket,
78+
socket: SecureSocket,
7279
};
7380

7481
try t.entry(
75-
EntryParams{ .router = &router, .socket = socket },
82+
EntryParams{ .router = &router, .socket = secure },
7683
struct {
7784
fn entry(rt: *Runtime, p: EntryParams) !void {
78-
var server = Server.init(rt.allocator, .{
79-
.security = .{ .tls = .{
80-
.cert = .{ .file = .{ .path = "./examples/tls/certs/cert.pem" } },
81-
.key = .{ .file = .{ .path = "./examples/tls/certs/key.pem" } },
82-
.cert_name = "CERTIFICATE",
83-
.key_name = "EC PRIVATE KEY",
84-
} },
85-
.stack_size = 1024 * 1024 * 8,
86-
});
87-
try server.serve(rt, p.router, p.socket);
85+
var server = Server.init(.{ .stack_size = 1024 * 1024 * 8 });
86+
try server.serve(rt, p.router, .{ .secure = p.socket });
8887
}
8988
}.entry,
9089
);

examples/unix/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ pub fn main() !void {
5353
EntryParams{ .router = &router, .socket = socket },
5454
struct {
5555
fn entry(rt: *Runtime, p: EntryParams) !void {
56-
var server = Server.init(rt.allocator, .{});
57-
try server.serve(rt, p.router, p.socket);
56+
var server = Server.init(.{});
57+
try server.serve(rt, p.router, .{ .normal = p.socket });
5858
}
5959
}.entry,
6060
);

0 commit comments

Comments
 (0)