--- sys/src/cmd/hget.c
+++ sys/src/cmd/hget.c
@@ -351,16 +351,28 @@ dohttp(URL *u, URL *px, Range *r, Out *out, long mtime
if(u->method == Https){
int tfd;
TLSconn conn;
+ static PEMChain *rootca;
memset(&conn, 0, sizeof conn);
conn.serverName = u->host; /* SNI: server requires it */
+ /*
+ * Decode the anchors once per process: redirects and
+ * retries re-enter here, and readcertchain re-reads and
+ * re-decodes the whole bundle without freeing the file
+ * image it decoded from. nil leaves tlsClient on the
+ * thumbprint-only trust model.
+ */
+ if(rootca == nil)
+ rootca = readcertchain("/sys/lib/tls/ca.pem");
+ conn.rootCAchain = rootca;
+ if(conn.rootCAchain == nil)
+ fprint(2, "hget: no CA bundle at /sys/lib/tls/ca.pem; chain validation disabled\n");
tfd = tlsClient(fd, &conn);
if(tfd < 0){
fprint(2, "tlsClient: %r\n");
close(fd);
return Error;
}
- /* BUG: check cert here? */
if(conn.cert)
free(conn.cert);
close(fd);
--- sys/src/cmd/webfs/io.c
+++ sys/src/cmd/webfs/io.c
@@ -48,6 +48,7 @@ _iotlsdial(va_list *arg)
char *addr, *local, *dir, *host;
int *cfdp, fd, tfd, usetls;
TLSconn conn;
+ static PEMChain *rootca;
addr = va_arg(*arg, char*);
local = va_arg(*arg, char*);
@@ -64,13 +65,24 @@ _iotlsdial(va_list *arg)
memset(&conn, 0, sizeof conn);
conn.serverName = host; /* SNI: server picks cert by it */
+ /*
+ * Decode the anchors once per process: they are read-only during
+ * verification, and readcertchain re-reads and re-decodes the whole
+ * bundle without freeing the file image it decoded from. A lost
+ * race between io procs costs a spare chain, never a nil one.
+ * nil leaves tlsClient on the thumbprint-only trust model.
+ */
+ if(rootca == nil)
+ rootca = readcertchain("/sys/lib/tls/ca.pem");
+ conn.rootCAchain = rootca;
+ if(conn.rootCAchain == nil)
+ fprint(2, "%s: no CA bundle at /sys/lib/tls/ca.pem; chain validation disabled\n", argv0);
tfd = tlsClient(fd, &conn);
close(fd);
if(tfd < 0)
fprint(2, "%s: tlsClient: %r\n", argv0);
else {
- /* BUG: check cert here? */
if(conn.cert)
free(conn.cert);
}
|