Build the BusyBox wget TLS helper and verify peer identity
@@ -0,0 +1,22 @@
1+defpkgbuild(ssl-client,
2+ #:pkgname, "ssl-client",
3+ #:pkgver, "1.37.0",
4+ #:pkgrel, 1,
5+ #:pkgdesc, "TLS helper for BusyBox wget with certificate and hostname verification",
6+ #:arch, [#:x86-64, #:aarch64],
7+ #:url, "https://gitlab.alpinelinux.org/alpine/aports",
8+ #:license, "GPL-2.0-only",
9+ #:depends, ["musl", "openssl", "ca-certificates-bundle"],
10+ #:source, ["https://raw.githubusercontent.com/alpinelinux/aports/a7de3e63cc8015474a4feadd8bb7c0ecc097b1fa/main/busybox/ssl_client.c",
11+ "check.py"],
12+ #:sha256sums, ["f36fb83637a12fe0706f828a3e9a4181ac6ed452b59d5d30fcf4dbe1884b8aed",
13+ "6114d569cef266336ebc8a47428860ce3d6849c5c338bf528f9065673703714d"],
14+ #:build, fn() {
15+ run("vinix-cc", "-O2", "ssl_client.c", "-lssl", "-lcrypto", "-o", "ssl_client")
16+ },
17+ #:check, fn() {
18+ run("python3", "check.py")
19+ },
20+ #:package, fn() {
21+ run("install", "-Dm755", "ssl_client", string-append(pkgdir(), "/usr/bin/ssl_client"))
22+ })
@@ -0,0 +1,70 @@
1+"""Exercise the BusyBox wget TLS helper against a private local peer."""
2+
3+import os
4+import socket
5+import ssl
6+import subprocess
7+from concurrent.futures import ThreadPoolExecutor
8+from pathlib import Path
9+
10+
11+def make_cert(name):
12+ subprocess.run(
13+ ["openssl", "req", "-x509", "-newkey", "ec", "-pkeyopt",
14+ "ec_paramgen_curve:P-256", "-nodes", "-days", "1",
15+ "-subj", f"/CN={name}", "-addext", f"subjectAltName=DNS:{name}",
16+ "-keyout", f"{name}.key", "-out", f"{name}.crt"],
17+ check=True, capture_output=True,
18+ )
19+
20+
21+def serve(connection, context, expect_success):
22+ connection.settimeout(8)
23+ try:
24+ with context.wrap_socket(connection, server_side=True) as peer:
25+ if not expect_success:
26+ raise AssertionError("client accepted the invalid peer")
27+ with peer.makefile("rb") as data:
28+ assert data.readline() == b"ping\n"
29+ peer.sendall(b"pong\n")
30+ # Send close_notify. The one-shot client may exit without replying.
31+ try:
32+ peer.unwrap().close()
33+ except ssl.SSLEOFError:
34+ pass
35+ except ssl.SSLError:
36+ if expect_success:
37+ raise
38+ finally:
39+ connection.close()
40+
41+
42+def check(context, hostname, trust, expect_success):
43+ client, server = socket.socketpair()
44+ with client, ThreadPoolExecutor(max_workers=1) as pool:
45+ task = pool.submit(serve, server, context, expect_success)
46+ try:
47+ result = subprocess.run(
48+ ["vinix-run", "./ssl_client", "-s", str(client.fileno()), "-n", hostname],
49+ input=b"ping\n", capture_output=True, pass_fds=(client.fileno(),),
50+ env={**os.environ, "SSL_CERT_FILE": str(Path(trust).resolve()),
51+ "SSL_CERT_DIR": str(Path("empty-certs").resolve())},
52+ timeout=10,
53+ )
54+ finally:
55+ client.close()
56+ task.result(timeout=10)
57+ if expect_success:
58+ assert result.returncode == 0 and result.stdout == b"pong\n", result.stderr
59+ else:
60+ assert result.returncode != 0 and b"certificate verify failed" in result.stderr, result.stderr
61+
62+
63+make_cert("vinix.test")
64+make_cert("other.test")
65+Path("empty-certs").mkdir()
66+context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
67+context.load_cert_chain("vinix.test.crt", "vinix.test.key")
68+check(context, "vinix.test.", "vinix.test.crt", True)
69+check(context, "wrong.test", "vinix.test.crt", False)
70+check(context, "vinix.test", "other.test.crt", False)