Skip to content

Commit

Permalink
feat(unstable): ALPN config in startTls/listenTls
Browse files Browse the repository at this point in the history
This commit adds the ability for users to configure ALPN protocols when
calling `Deno.startTls` and `Deno.listenTls`.
  • Loading branch information
lucacasonato committed Apr 8, 2021
1 parent 70af812 commit ba443aa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
14 changes: 14 additions & 0 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,10 @@ declare namespace Deno {
hostname?: string;
/** Server certificate file. */
certFile?: string;
/** Application-Layer Protocol Negotiation (ALPN) protocols to announce to
* the client. If not specified, no ALPN extension will be included in the
* TLS handshake. */
alpnProtocols?: string[];
}

/** **UNSTABLE**: new API, yet to be vetted.
Expand All @@ -1001,6 +1005,16 @@ declare namespace Deno {
options?: StartTlsOptions,
): Promise<Conn>;

export interface ListenTlsOptions {
/** **UNSTABLE**: new API, yet to be vetted.
*
* Application-Layer Protocol Negotiation (ALPN) protocols to announce to
* the client. If not specified, no ALPN extension will be included in the
* TLS handshake.
*/
alpnProtocols?: string[];
}

/** **UNSTABLE**: The `signo` argument may change to require the Deno.Signal
* enum.
*
Expand Down
5 changes: 4 additions & 1 deletion runtime/js/40_tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,28 @@
keyFile,
hostname = "0.0.0.0",
transport = "tcp",
alpnProtocols,
}) {
const res = opListenTls({
port,
certFile,
keyFile,
hostname,
transport,
alpnProtocols,
});
return new TLSListener(res.rid, res.localAddr);
}

async function startTls(
conn,
{ hostname = "127.0.0.1", certFile } = {},
{ hostname = "127.0.0.1", certFile, alpnProtocols } = {},
) {
const res = await opStartTls({
rid: conn.rid,
hostname,
certFile,
alpnProtocols,
});
return new Conn(res.rid, res.remoteAddr, res.localAddr);
}
Expand Down
11 changes: 11 additions & 0 deletions runtime/ops/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct StartTlsArgs {
rid: ResourceId,
cert_file: Option<String>,
hostname: String,
alpn_protocols: Option<Vec<String>>,
}

async fn op_start_tls(
Expand Down Expand Up @@ -132,6 +133,10 @@ async fn op_start_tls(
config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
if let Some(alpn_protocols) = args.alpn_protocols {
config.alpn_protocols =
alpn_protocols.into_iter().map(|s| s.into_bytes()).collect();
}
if let Some(path) = args.cert_file {
let key_file = File::open(path)?;
let reader = &mut BufReader::new(key_file);
Expand Down Expand Up @@ -300,6 +305,7 @@ pub struct ListenTlsArgs {
port: u16,
cert_file: String,
key_file: String,
alpn_protocols: Option<Vec<String>>,
}

fn op_listen_tls(
Expand All @@ -318,6 +324,11 @@ fn op_listen_tls(
permissions.read.check(Path::new(&key_file))?;
}
let mut config = ServerConfig::new(NoClientAuth::new());
if let Some(alpn_protocols) = args.alpn_protocols {
super::check_unstable(state, "Deno.listenTls#alpn_protocols");
config.alpn_protocols =
alpn_protocols.into_iter().map(|s| s.into_bytes()).collect();
}
config
.set_single_cert(load_certs(&cert_file)?, load_keys(&key_file)?.remove(0))
.expect("invalid key or certificate");
Expand Down

0 comments on commit ba443aa

Please sign in to comment.