2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-06-28 19:43:03 -04:00
|
|
|
use crate::io::TcpStreamResource;
|
|
|
|
use crate::ops::IpAddr;
|
2021-11-26 13:59:53 -05:00
|
|
|
use crate::ops::TlsHandshakeInfo;
|
2024-04-08 18:18:14 -04:00
|
|
|
use crate::raw::NetworkListenerResource;
|
2019-09-23 14:40:38 -04:00
|
|
|
use crate::resolve_addr::resolve_addr;
|
2020-12-15 07:02:26 -05:00
|
|
|
use crate::resolve_addr::resolve_addr_sync;
|
2024-04-08 18:18:14 -04:00
|
|
|
use crate::tcp::TcpListener;
|
2021-07-22 06:28:46 -04:00
|
|
|
use crate::DefaultTlsOptions;
|
2021-06-28 19:43:03 -04:00
|
|
|
use crate::NetPermissions;
|
2021-08-10 07:19:45 -04:00
|
|
|
use crate::UnsafelyIgnoreCertificateErrors;
|
2024-04-18 13:21:08 -04:00
|
|
|
use deno_core::anyhow::anyhow;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
use deno_core::anyhow::bail;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::bad_resource;
|
|
|
|
use deno_core::error::custom_error;
|
2020-12-29 20:19:28 -05:00
|
|
|
use deno_core::error::generic_error;
|
2021-04-26 15:39:55 -04:00
|
|
|
use deno_core::error::invalid_hostname;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2023-09-12 09:39:21 -04:00
|
|
|
use deno_core::op2;
|
2024-04-08 17:01:02 -04:00
|
|
|
use deno_core::v8;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
2021-11-09 13:26:17 -05:00
|
|
|
use deno_core::AsyncResult;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::CancelHandle;
|
|
|
|
use deno_core::CancelTryFuture;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
2021-08-07 08:49:38 -04:00
|
|
|
use deno_tls::create_client_config;
|
2021-08-25 08:25:12 -04:00
|
|
|
use deno_tls::load_certs;
|
|
|
|
use deno_tls::load_private_keys;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
use deno_tls::new_resolver;
|
2024-07-01 20:09:47 -04:00
|
|
|
use deno_tls::rustls::pki_types::ServerName;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
use deno_tls::rustls::ClientConnection;
|
2021-08-07 08:49:38 -04:00
|
|
|
use deno_tls::rustls::ServerConfig;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
use deno_tls::ServerConfigProvider;
|
2023-11-01 17:11:01 -04:00
|
|
|
use deno_tls::SocketUse;
|
2024-04-08 17:01:02 -04:00
|
|
|
use deno_tls::TlsKey;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
use deno_tls::TlsKeyLookup;
|
2024-04-08 17:01:02 -04:00
|
|
|
use deno_tls::TlsKeys;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
use deno_tls::TlsKeysHolder;
|
2023-11-15 18:12:46 -05:00
|
|
|
use rustls_tokio_stream::TlsStreamRead;
|
|
|
|
use rustls_tokio_stream::TlsStreamWrite;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2019-09-23 14:40:38 -04:00
|
|
|
use std::convert::From;
|
2019-10-21 14:38:28 -04:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufReader;
|
2021-05-10 19:49:57 -04:00
|
|
|
use std::io::ErrorKind;
|
2024-04-08 17:01:02 -04:00
|
|
|
use std::io::Read;
|
2024-04-08 18:18:14 -04:00
|
|
|
use std::net::SocketAddr;
|
2023-11-15 18:12:46 -05:00
|
|
|
use std::num::NonZeroUsize;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2019-09-23 14:40:38 -04:00
|
|
|
use std::sync::Arc;
|
2021-10-19 19:30:04 -04:00
|
|
|
use tokio::io::AsyncReadExt;
|
|
|
|
use tokio::io::AsyncWriteExt;
|
2019-09-23 14:40:38 -04:00
|
|
|
use tokio::net::TcpStream;
|
2021-01-12 19:22:33 -05:00
|
|
|
|
2023-11-15 18:12:46 -05:00
|
|
|
pub use rustls_tokio_stream::TlsStream;
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2023-11-15 18:12:46 -05:00
|
|
|
pub(crate) const TLS_BUFFER_SIZE: Option<NonZeroUsize> =
|
|
|
|
NonZeroUsize::new(65536);
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2024-04-08 18:18:14 -04:00
|
|
|
pub struct TlsListener {
|
|
|
|
pub(crate) tcp_listener: TcpListener,
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
pub(crate) tls_config: Option<Arc<ServerConfig>>,
|
|
|
|
pub(crate) server_config_provider: Option<ServerConfigProvider>,
|
2024-04-08 18:18:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TlsListener {
|
|
|
|
pub async fn accept(&self) -> std::io::Result<(TlsStream, SocketAddr)> {
|
|
|
|
let (tcp, addr) = self.tcp_listener.accept().await?;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
let tls = if let Some(provider) = &self.server_config_provider {
|
|
|
|
TlsStream::new_server_side_acceptor(
|
|
|
|
tcp,
|
|
|
|
provider.clone(),
|
|
|
|
TLS_BUFFER_SIZE,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
TlsStream::new_server_side(
|
|
|
|
tcp,
|
|
|
|
self.tls_config.clone().unwrap(),
|
|
|
|
TLS_BUFFER_SIZE,
|
|
|
|
)
|
|
|
|
};
|
2024-04-08 18:18:14 -04:00
|
|
|
Ok((tls, addr))
|
|
|
|
}
|
|
|
|
pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
|
|
|
|
self.tcp_listener.local_addr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:30:04 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TlsStreamResource {
|
2023-11-15 18:12:46 -05:00
|
|
|
rd: AsyncRefCell<TlsStreamRead>,
|
|
|
|
wr: AsyncRefCell<TlsStreamWrite>,
|
2021-11-26 13:59:53 -05:00
|
|
|
// `None` when a TLS handshake hasn't been done.
|
|
|
|
handshake_info: RefCell<Option<TlsHandshakeInfo>>,
|
2021-10-19 19:30:04 -04:00
|
|
|
cancel_handle: CancelHandle, // Only read and handshake ops get canceled.
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TlsStreamResource {
|
2023-11-15 18:12:46 -05:00
|
|
|
pub fn new((rd, wr): (TlsStreamRead, TlsStreamWrite)) -> Self {
|
2021-10-19 19:30:04 -04:00
|
|
|
Self {
|
|
|
|
rd: rd.into(),
|
|
|
|
wr: wr.into(),
|
2021-11-26 13:59:53 -05:00
|
|
|
handshake_info: RefCell::new(None),
|
2021-10-19 19:30:04 -04:00
|
|
|
cancel_handle: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 18:12:46 -05:00
|
|
|
pub fn into_inner(self) -> (TlsStreamRead, TlsStreamWrite) {
|
2021-10-19 19:30:04 -04:00
|
|
|
(self.rd.into_inner(), self.wr.into_inner())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read(
|
2021-11-09 13:26:17 -05:00
|
|
|
self: Rc<Self>,
|
2022-10-09 10:49:25 -04:00
|
|
|
data: &mut [u8],
|
|
|
|
) -> Result<usize, AnyError> {
|
2021-11-09 13:26:17 -05:00
|
|
|
let mut rd = RcRef::map(&self, |r| &r.rd).borrow_mut().await;
|
|
|
|
let cancel_handle = RcRef::map(&self, |r| &r.cancel_handle);
|
2023-11-15 18:12:46 -05:00
|
|
|
Ok(rd.read(data).try_or_cancel(cancel_handle).await?)
|
2021-10-19 19:30:04 -04:00
|
|
|
}
|
|
|
|
|
2022-10-09 10:49:25 -04:00
|
|
|
pub async fn write(self: Rc<Self>, data: &[u8]) -> Result<usize, AnyError> {
|
2021-10-19 19:30:04 -04:00
|
|
|
let mut wr = RcRef::map(self, |r| &r.wr).borrow_mut().await;
|
2022-10-09 10:49:25 -04:00
|
|
|
let nwritten = wr.write(data).await?;
|
2021-10-19 19:30:04 -04:00
|
|
|
wr.flush().await?;
|
|
|
|
Ok(nwritten)
|
|
|
|
}
|
|
|
|
|
2021-11-09 13:26:17 -05:00
|
|
|
pub async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> {
|
2021-10-19 19:30:04 -04:00
|
|
|
let mut wr = RcRef::map(self, |r| &r.wr).borrow_mut().await;
|
|
|
|
wr.shutdown().await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:53 -05:00
|
|
|
pub async fn handshake(
|
|
|
|
self: &Rc<Self>,
|
|
|
|
) -> Result<TlsHandshakeInfo, AnyError> {
|
|
|
|
if let Some(tls_info) = &*self.handshake_info.borrow() {
|
|
|
|
return Ok(tls_info.clone());
|
2021-10-19 19:30:04 -04:00
|
|
|
}
|
2021-11-26 13:59:53 -05:00
|
|
|
|
|
|
|
let mut wr = RcRef::map(self, |r| &r.wr).borrow_mut().await;
|
|
|
|
let cancel_handle = RcRef::map(self, |r| &r.cancel_handle);
|
2023-11-15 18:12:46 -05:00
|
|
|
let handshake = wr.handshake().try_or_cancel(cancel_handle).await?;
|
2021-11-26 13:59:53 -05:00
|
|
|
|
2023-11-15 18:12:46 -05:00
|
|
|
let alpn_protocol = handshake.alpn.map(|alpn| alpn.into());
|
2021-11-26 13:59:53 -05:00
|
|
|
let tls_info = TlsHandshakeInfo { alpn_protocol };
|
|
|
|
self.handshake_info.replace(Some(tls_info.clone()));
|
|
|
|
Ok(tls_info)
|
2021-10-19 19:30:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for TlsStreamResource {
|
2022-10-09 10:49:25 -04:00
|
|
|
deno_core::impl_readable_byob!();
|
|
|
|
deno_core::impl_writable!();
|
|
|
|
|
2021-10-19 19:30:04 -04:00
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"tlsStream".into()
|
|
|
|
}
|
|
|
|
|
2021-11-09 13:26:17 -05:00
|
|
|
fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
|
|
|
|
Box::pin(self.shutdown())
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:30:04 -04:00
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel_handle.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 14:40:38 -04:00
|
|
|
#[derive(Deserialize)]
|
2019-10-21 14:38:28 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-25 14:17:37 -04:00
|
|
|
pub struct ConnectTlsArgs {
|
2019-10-21 14:38:28 -04:00
|
|
|
cert_file: Option<String>,
|
2021-09-30 03:26:15 -04:00
|
|
|
ca_certs: Vec<String>,
|
2021-11-26 13:59:53 -05:00
|
|
|
alpn_protocols: Option<Vec<String>>,
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
server_name: Option<String>,
|
2019-10-13 10:37:37 -04:00
|
|
|
}
|
2019-09-23 14:40:38 -04:00
|
|
|
|
2020-04-18 11:21:20 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-11-08 20:07:12 -05:00
|
|
|
pub struct StartTlsArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2021-09-30 03:26:15 -04:00
|
|
|
ca_certs: Vec<String>,
|
2020-04-18 11:21:20 -04:00
|
|
|
hostname: String,
|
2021-11-26 13:59:53 -05:00
|
|
|
alpn_protocols: Option<Vec<String>>,
|
2020-04-18 11:21:20 -04:00
|
|
|
}
|
|
|
|
|
2024-04-08 17:01:02 -04:00
|
|
|
#[op2]
|
2024-06-21 01:19:39 -04:00
|
|
|
#[cppgc]
|
|
|
|
pub fn op_tls_key_null() -> TlsKeysHolder {
|
|
|
|
TlsKeysHolder::from(TlsKeys::Null)
|
2024-04-08 17:01:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op2]
|
2024-06-21 01:19:39 -04:00
|
|
|
#[cppgc]
|
|
|
|
pub fn op_tls_key_static(
|
|
|
|
#[string] cert: &str,
|
|
|
|
#[string] key: &str,
|
|
|
|
) -> Result<TlsKeysHolder, AnyError> {
|
2024-04-08 17:01:02 -04:00
|
|
|
let cert = load_certs(&mut BufReader::new(cert.as_bytes()))?;
|
|
|
|
let key = load_private_keys(key.as_bytes())?
|
|
|
|
.into_iter()
|
|
|
|
.next()
|
|
|
|
.unwrap();
|
2024-06-21 01:19:39 -04:00
|
|
|
Ok(TlsKeysHolder::from(TlsKeys::Static(TlsKey(cert, key))))
|
2024-04-08 17:01:02 -04:00
|
|
|
}
|
|
|
|
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
#[op2]
|
|
|
|
pub fn op_tls_cert_resolver_create<'s>(
|
|
|
|
scope: &mut v8::HandleScope<'s>,
|
|
|
|
) -> v8::Local<'s, v8::Array> {
|
|
|
|
let (resolver, lookup) = new_resolver();
|
|
|
|
let resolver = deno_core::cppgc::make_cppgc_object(
|
|
|
|
scope,
|
|
|
|
TlsKeysHolder::from(TlsKeys::Resolver(resolver)),
|
|
|
|
);
|
|
|
|
let lookup = deno_core::cppgc::make_cppgc_object(scope, lookup);
|
|
|
|
v8::Array::new_with_elements(scope, &[resolver.into(), lookup.into()])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(async)]
|
|
|
|
#[string]
|
|
|
|
pub async fn op_tls_cert_resolver_poll(
|
|
|
|
#[cppgc] lookup: &TlsKeyLookup,
|
|
|
|
) -> Option<String> {
|
|
|
|
lookup.poll().await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_tls_cert_resolver_resolve(
|
|
|
|
#[cppgc] lookup: &TlsKeyLookup,
|
|
|
|
#[string] sni: String,
|
|
|
|
#[cppgc] key: &TlsKeysHolder,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let TlsKeys::Static(key) = key.take() else {
|
|
|
|
bail!("unexpected key type");
|
|
|
|
};
|
|
|
|
lookup.resolve(sni, Ok(key));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_tls_cert_resolver_resolve_error(
|
|
|
|
#[cppgc] lookup: &TlsKeyLookup,
|
|
|
|
#[string] sni: String,
|
|
|
|
#[string] error: String,
|
|
|
|
) {
|
|
|
|
lookup.resolve(sni, Err(anyhow!(error)))
|
|
|
|
}
|
|
|
|
|
2024-03-11 23:48:00 -04:00
|
|
|
#[op2]
|
2023-09-12 09:39:21 -04:00
|
|
|
#[serde]
|
2024-03-11 23:48:00 -04:00
|
|
|
pub fn op_tls_start<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[serde] args: StartTlsArgs,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError>
|
2021-06-28 19:43:03 -04:00
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2021-05-10 19:49:57 -04:00
|
|
|
let hostname = match &*args.hostname {
|
2024-07-01 20:09:47 -04:00
|
|
|
"" => "localhost".to_string(),
|
|
|
|
n => n.to_string(),
|
2021-05-10 19:49:57 -04:00
|
|
|
};
|
2021-10-29 11:13:31 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut s = state.borrow_mut();
|
2021-06-28 19:43:03 -04:00
|
|
|
let permissions = s.borrow_mut::<NP>();
|
2024-07-01 20:09:47 -04:00
|
|
|
permissions.check_net(&(&hostname, Some(0)), "Deno.startTls()")?;
|
2020-04-28 12:01:13 -04:00
|
|
|
}
|
2020-04-18 11:21:20 -04:00
|
|
|
|
2021-10-29 11:13:31 -04:00
|
|
|
let ca_certs = args
|
2021-09-30 03:26:15 -04:00
|
|
|
.ca_certs
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| s.into_bytes())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2024-07-01 20:09:47 -04:00
|
|
|
let hostname_dns = ServerName::try_from(hostname.to_string())
|
|
|
|
.map_err(|_| invalid_hostname(&hostname))?;
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2021-08-10 08:19:42 -04:00
|
|
|
let unsafely_ignore_certificate_errors = state
|
|
|
|
.borrow()
|
2021-09-22 05:12:08 -04:00
|
|
|
.try_borrow::<UnsafelyIgnoreCertificateErrors>()
|
|
|
|
.and_then(|it| it.0.clone());
|
2021-08-10 08:19:42 -04:00
|
|
|
|
2021-08-07 08:49:38 -04:00
|
|
|
let root_cert_store = state
|
|
|
|
.borrow()
|
|
|
|
.borrow::<DefaultTlsOptions>()
|
2023-05-01 16:42:05 -04:00
|
|
|
.root_cert_store()?;
|
2022-10-17 22:28:27 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource_rc = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.take::<TcpStreamResource>(rid)?;
|
2022-10-17 22:28:27 -04:00
|
|
|
// This TCP connection might be used somewhere else. If it's the case, we cannot proceed with the
|
2024-09-27 10:07:20 -04:00
|
|
|
// process of starting a TLS connection on top of this TCP connection, so we just return a Busy error.
|
|
|
|
// See also: https://github.com/denoland/deno/pull/16242
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = Rc::try_unwrap(resource_rc)
|
2024-09-27 10:07:20 -04:00
|
|
|
.map_err(|_| custom_error("Busy", "TCP stream is currently in use"))?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let (read_half, write_half) = resource.into_inner();
|
|
|
|
let tcp_stream = read_half.reunite(write_half)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2021-11-26 13:59:53 -05:00
|
|
|
let mut tls_config = create_client_config(
|
2021-08-10 08:19:42 -04:00
|
|
|
root_cert_store,
|
2021-09-30 03:26:15 -04:00
|
|
|
ca_certs,
|
2021-08-10 08:19:42 -04:00
|
|
|
unsafely_ignore_certificate_errors,
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
TlsKeys::Null,
|
2023-11-01 17:11:01 -04:00
|
|
|
SocketUse::GeneralSsl,
|
2021-11-26 13:59:53 -05:00
|
|
|
)?;
|
|
|
|
|
|
|
|
if let Some(alpn_protocols) = args.alpn_protocols {
|
|
|
|
tls_config.alpn_protocols =
|
|
|
|
alpn_protocols.into_iter().map(|s| s.into_bytes()).collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
let tls_config = Arc::new(tls_config);
|
2023-11-15 18:12:46 -05:00
|
|
|
let tls_stream = TlsStream::new_client_side(
|
|
|
|
tcp_stream,
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
ClientConnection::new(tls_config, hostname_dns)?,
|
2023-11-15 18:12:46 -05:00
|
|
|
TLS_BUFFER_SIZE,
|
|
|
|
);
|
2020-12-16 11:14:12 -05:00
|
|
|
|
|
|
|
let rid = {
|
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
state_
|
|
|
|
.resource_table
|
2021-05-10 19:49:57 -04:00
|
|
|
.add(TlsStreamResource::new(tls_stream.into_split()))
|
2020-12-16 11:14:12 -05:00
|
|
|
};
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr)))
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2022-10-25 16:50:55 -04:00
|
|
|
pub async fn op_net_connect_tls<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[serde] addr: IpAddr,
|
|
|
|
#[serde] args: ConnectTlsArgs,
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
#[cppgc] key_pair: &TlsKeysHolder,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError>
|
2021-06-28 19:43:03 -04:00
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2021-05-10 19:49:57 -04:00
|
|
|
let cert_file = args.cert_file.as_deref();
|
2021-08-10 07:19:45 -04:00
|
|
|
let unsafely_ignore_certificate_errors = state
|
2021-08-09 10:53:21 -04:00
|
|
|
.borrow()
|
2021-09-22 05:12:08 -04:00
|
|
|
.try_borrow::<UnsafelyIgnoreCertificateErrors>()
|
|
|
|
.and_then(|it| it.0.clone());
|
2021-08-09 09:55:00 -04:00
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
let cert_file = {
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut s = state.borrow_mut();
|
2021-06-28 19:43:03 -04:00
|
|
|
let permissions = s.borrow_mut::<NP>();
|
2022-10-25 16:50:55 -04:00
|
|
|
permissions
|
|
|
|
.check_net(&(&addr.hostname, Some(addr.port)), "Deno.connectTls()")?;
|
2021-05-10 19:49:57 -04:00
|
|
|
if let Some(path) = cert_file {
|
2024-09-16 16:39:37 -04:00
|
|
|
Some(permissions.check_read(path, "Deno.connectTls()")?)
|
|
|
|
} else {
|
|
|
|
None
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2024-09-16 16:39:37 -04:00
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2021-09-30 03:26:15 -04:00
|
|
|
let mut ca_certs = args
|
|
|
|
.ca_certs
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| s.into_bytes())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if let Some(path) = cert_file {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
File::open(path)?.read_to_end(&mut buf)?;
|
|
|
|
ca_certs.push(buf);
|
2021-08-07 08:49:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let root_cert_store = state
|
|
|
|
.borrow()
|
|
|
|
.borrow::<DefaultTlsOptions>()
|
2023-05-01 16:42:05 -04:00
|
|
|
.root_cert_store()?;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
let hostname_dns = if let Some(server_name) = args.server_name {
|
2024-07-01 20:09:47 -04:00
|
|
|
ServerName::try_from(server_name)
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
} else {
|
2024-07-01 20:09:47 -04:00
|
|
|
ServerName::try_from(addr.hostname.clone())
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
}
|
|
|
|
.map_err(|_| invalid_hostname(&addr.hostname))?;
|
2022-10-25 16:50:55 -04:00
|
|
|
let connect_addr = resolve_addr(&addr.hostname, addr.port)
|
2020-12-29 20:19:28 -05:00
|
|
|
.await?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
2021-05-10 19:49:57 -04:00
|
|
|
let tcp_stream = TcpStream::connect(connect_addr).await?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2021-12-06 18:48:11 -05:00
|
|
|
|
2021-08-09 10:53:21 -04:00
|
|
|
let mut tls_config = create_client_config(
|
|
|
|
root_cert_store,
|
2021-09-30 03:26:15 -04:00
|
|
|
ca_certs,
|
2021-08-10 07:19:45 -04:00
|
|
|
unsafely_ignore_certificate_errors,
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
key_pair.take(),
|
2023-11-01 17:11:01 -04:00
|
|
|
SocketUse::GeneralSsl,
|
2021-08-09 10:53:21 -04:00
|
|
|
)?;
|
2021-08-09 09:55:00 -04:00
|
|
|
|
2021-11-26 13:59:53 -05:00
|
|
|
if let Some(alpn_protocols) = args.alpn_protocols {
|
|
|
|
tls_config.alpn_protocols =
|
|
|
|
alpn_protocols.into_iter().map(|s| s.into_bytes()).collect();
|
|
|
|
}
|
|
|
|
|
2021-08-09 09:55:00 -04:00
|
|
|
let tls_config = Arc::new(tls_config);
|
|
|
|
|
2023-11-15 18:12:46 -05:00
|
|
|
let tls_stream = TlsStream::new_client_side(
|
|
|
|
tcp_stream,
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
ClientConnection::new(tls_config, hostname_dns)?,
|
2023-11-15 18:12:46 -05:00
|
|
|
TLS_BUFFER_SIZE,
|
|
|
|
);
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
let rid = {
|
|
|
|
let mut state_ = state.borrow_mut();
|
2020-12-16 11:14:12 -05:00
|
|
|
state_
|
|
|
|
.resource_table
|
2021-05-10 19:49:57 -04:00
|
|
|
.add(TlsStreamResource::new(tls_stream.into_split()))
|
2020-09-10 09:57:45 -04:00
|
|
|
};
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr)))
|
2019-09-23 14:40:38 -04:00
|
|
|
}
|
2019-10-21 14:38:28 -04:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct ListenTlsArgs {
|
2021-04-10 16:04:44 -04:00
|
|
|
alpn_protocols: Option<Vec<String>>,
|
2022-10-26 15:04:27 -04:00
|
|
|
reuse_port: bool,
|
2024-08-14 18:26:21 -04:00
|
|
|
#[serde(default)]
|
|
|
|
load_balanced: bool,
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-10-25 16:50:55 -04:00
|
|
|
pub fn op_net_listen_tls<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[serde] addr: IpAddr,
|
|
|
|
#[serde] args: ListenTlsArgs,
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
#[cppgc] keys: &TlsKeysHolder,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<(ResourceId, IpAddr), AnyError>
|
2021-06-28 19:43:03 -04:00
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2022-10-26 15:04:27 -04:00
|
|
|
if args.reuse_port {
|
|
|
|
super::check_unstable(state, "Deno.listenTls({ reusePort: true })");
|
|
|
|
}
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
2021-06-28 19:43:03 -04:00
|
|
|
let permissions = state.borrow_mut::<NP>();
|
2022-10-25 16:50:55 -04:00
|
|
|
permissions
|
|
|
|
.check_net(&(&addr.hostname, Some(addr.port)), "Deno.listenTls()")?;
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
let bind_addr = resolve_addr_sync(&addr.hostname, addr.port)?
|
2020-12-29 20:19:28 -05:00
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
2024-04-08 18:18:14 -04:00
|
|
|
|
2024-08-14 18:26:21 -04:00
|
|
|
let tcp_listener = if args.load_balanced {
|
|
|
|
TcpListener::bind_load_balanced(bind_addr)
|
|
|
|
} else {
|
|
|
|
TcpListener::bind_direct(bind_addr, args.reuse_port)
|
|
|
|
}?;
|
2021-05-10 19:49:57 -04:00
|
|
|
let local_addr = tcp_listener.local_addr()?;
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
let alpn = args
|
|
|
|
.alpn_protocols
|
|
|
|
.unwrap_or_default()
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| s.into_bytes())
|
|
|
|
.collect();
|
|
|
|
let listener = match keys.take() {
|
|
|
|
TlsKeys::Null => Err(anyhow!("Deno.listenTls requires a key")),
|
|
|
|
TlsKeys::Static(TlsKey(cert, key)) => {
|
|
|
|
let mut tls_config = ServerConfig::builder()
|
|
|
|
.with_no_client_auth()
|
|
|
|
.with_single_cert(cert, key)
|
|
|
|
.map_err(|e| anyhow!(e))?;
|
|
|
|
tls_config.alpn_protocols = alpn;
|
|
|
|
Ok(TlsListener {
|
|
|
|
tcp_listener,
|
|
|
|
tls_config: Some(tls_config.into()),
|
|
|
|
server_config_provider: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
TlsKeys::Resolver(resolver) => Ok(TlsListener {
|
|
|
|
tcp_listener,
|
|
|
|
tls_config: None,
|
|
|
|
server_config_provider: Some(resolver.into_server_config_provider(alpn)),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
.map_err(|e| {
|
|
|
|
custom_error("InvalidData", "Error creating TLS certificate").context(e)
|
|
|
|
})?;
|
2021-05-10 19:49:57 -04:00
|
|
|
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
let tls_listener_resource = NetworkListenerResource::new(listener);
|
2020-04-21 09:48:44 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(tls_listener_resource);
|
2019-10-21 14:38:28 -04:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, IpAddr::from(local_addr)))
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2022-10-25 16:50:55 -04:00
|
|
|
pub async fn op_net_accept_tls(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError> {
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
2024-04-08 18:18:14 -04:00
|
|
|
.get::<NetworkListenerResource<TlsListener>>(rid)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| bad_resource("Listener has been closed"))?;
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2024-04-08 18:18:14 -04:00
|
|
|
let cancel_handle = RcRef::map(&resource, |r| &r.cancel);
|
|
|
|
let listener = RcRef::map(&resource, |r| &r.listener)
|
2020-12-16 11:14:12 -05:00
|
|
|
.try_borrow_mut()
|
|
|
|
.ok_or_else(|| custom_error("Busy", "Another accept task is ongoing"))?;
|
2021-05-10 19:49:57 -04:00
|
|
|
|
2024-04-08 18:18:14 -04:00
|
|
|
let (tls_stream, remote_addr) =
|
|
|
|
match listener.accept().try_or_cancel(&cancel_handle).await {
|
2021-05-10 19:49:57 -04:00
|
|
|
Ok(tuple) => tuple,
|
|
|
|
Err(err) if err.kind() == ErrorKind::Interrupted => {
|
|
|
|
return Err(bad_resource("Listener has been closed"));
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2021-05-10 19:49:57 -04:00
|
|
|
Err(err) => return Err(err.into()),
|
|
|
|
};
|
|
|
|
|
2024-04-08 18:18:14 -04:00
|
|
|
let local_addr = tls_stream.local_addr()?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let rid = {
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut state_ = state.borrow_mut();
|
2020-12-16 11:14:12 -05:00
|
|
|
state_
|
|
|
|
.resource_table
|
2021-05-10 19:49:57 -04:00
|
|
|
.add(TlsStreamResource::new(tls_stream.into_split()))
|
2020-08-28 11:08:24 -04:00
|
|
|
};
|
2020-12-16 11:14:12 -05:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr)))
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
2021-10-19 19:30:04 -04:00
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2021-11-08 20:07:12 -05:00
|
|
|
pub async fn op_tls_handshake(
|
2021-10-19 19:30:04 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2021-11-26 13:59:53 -05:00
|
|
|
) -> Result<TlsHandshakeInfo, AnyError> {
|
2021-10-19 19:30:04 -04:00
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
2024-04-08 18:18:14 -04:00
|
|
|
.get::<TlsStreamResource>(rid)
|
|
|
|
.map_err(|_| bad_resource("Listener has been closed"))?;
|
2021-10-19 19:30:04 -04:00
|
|
|
resource.handshake().await
|
|
|
|
}
|