2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const ops = core.ops;
|
2023-03-08 06:44:54 -05:00
|
|
|
import { Conn, Listener } from "ext:deno_net/01_net.js";
|
2023-07-25 02:26:18 -04:00
|
|
|
const { Number, TypeError } = primordials;
|
2023-12-26 20:30:26 -05:00
|
|
|
const {
|
|
|
|
op_tls_handshake,
|
|
|
|
op_tls_start,
|
|
|
|
op_net_accept_tls,
|
|
|
|
op_net_connect_tls,
|
|
|
|
} = core.ensureFastOps();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function opStartTls(args) {
|
2023-12-26 20:30:26 -05:00
|
|
|
return op_tls_start(args);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function opTlsHandshake(rid) {
|
2023-12-26 20:30:26 -05:00
|
|
|
return op_tls_handshake(rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class TlsConn extends Conn {
|
|
|
|
handshake() {
|
|
|
|
return opTlsHandshake(this.rid);
|
2021-10-26 16:27:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-10-26 16:27:47 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async function connectTls({
|
|
|
|
port,
|
|
|
|
hostname = "127.0.0.1",
|
|
|
|
transport = "tcp",
|
|
|
|
certFile = undefined,
|
|
|
|
caCerts = [],
|
|
|
|
certChain = undefined,
|
|
|
|
privateKey = undefined,
|
|
|
|
alpnProtocols = undefined,
|
|
|
|
}) {
|
|
|
|
if (transport !== "tcp") {
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
2021-10-26 16:27:47 -04:00
|
|
|
}
|
2023-12-26 20:30:26 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await op_net_connect_tls(
|
2023-02-07 14:22:46 -05:00
|
|
|
{ hostname, port },
|
|
|
|
{ certFile, caCerts, certChain, privateKey, alpnProtocols },
|
|
|
|
);
|
|
|
|
localAddr.transport = "tcp";
|
|
|
|
remoteAddr.transport = "tcp";
|
|
|
|
return new TlsConn(rid, remoteAddr, localAddr);
|
|
|
|
}
|
2021-10-26 16:27:47 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class TlsListener extends Listener {
|
|
|
|
async accept() {
|
2023-12-26 20:30:26 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await op_net_accept_tls(
|
2023-02-07 14:22:46 -05:00
|
|
|
this.rid,
|
2022-10-25 16:50:55 -04:00
|
|
|
);
|
|
|
|
localAddr.transport = "tcp";
|
|
|
|
remoteAddr.transport = "tcp";
|
|
|
|
return new TlsConn(rid, remoteAddr, localAddr);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function listenTls({
|
|
|
|
port,
|
|
|
|
cert,
|
|
|
|
certFile,
|
|
|
|
key,
|
|
|
|
keyFile,
|
|
|
|
hostname = "0.0.0.0",
|
|
|
|
transport = "tcp",
|
|
|
|
alpnProtocols = undefined,
|
|
|
|
reusePort = false,
|
|
|
|
}) {
|
|
|
|
if (transport !== "tcp") {
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const { 0: rid, 1: localAddr } = ops.op_net_listen_tls(
|
2023-07-25 02:26:18 -04:00
|
|
|
{ hostname, port: Number(port) },
|
2023-02-07 14:22:46 -05:00
|
|
|
{ cert, certFile, key, keyFile, alpnProtocols, reusePort },
|
|
|
|
);
|
|
|
|
return new TlsListener(rid, localAddr);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async function startTls(
|
|
|
|
conn,
|
|
|
|
{
|
|
|
|
hostname = "127.0.0.1",
|
|
|
|
certFile = undefined,
|
|
|
|
caCerts = [],
|
2021-11-26 13:59:53 -05:00
|
|
|
alpnProtocols = undefined,
|
2023-02-07 14:22:46 -05:00
|
|
|
} = {},
|
|
|
|
) {
|
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await opStartTls({
|
|
|
|
rid: conn.rid,
|
|
|
|
hostname,
|
|
|
|
certFile,
|
|
|
|
caCerts,
|
|
|
|
alpnProtocols,
|
|
|
|
});
|
|
|
|
return new TlsConn(rid, remoteAddr, localAddr);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { connectTls, listenTls, startTls, TlsConn, TlsListener };
|