Skip to content

Commit d9dbbf1

Browse files
committed
Start implementing TLS
1 parent 5f2e04e commit d9dbbf1

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

qbi-socket/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ rustls = "0.23.12"
1313
qb-core = { path = "../qb-core" }
1414
qb-proto = { path = "../qb-proto" }
1515
qb-ext = { path = "../qb-ext" }
16+
tokio-rustls = "0.26.0"

qbi-socket/src/lib.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TODO: rustls TLS impl for preventing MITM attacks
22

3-
use std::net::SocketAddr;
3+
use std::{net::SocketAddr, sync::Arc};
44

55
use bitcode::{Decode, Encode};
66
use qb_core::common::QBDeviceId;
@@ -9,8 +9,10 @@ use qb_ext::{
99
interface::{QBIChannel, QBIContext, QBIHostMessage, QBIMessage, QBISetup, QBISlaveMessage},
1010
};
1111
use qb_proto::QBP;
12+
use rustls::{pki_types::ServerName, ClientConfig, RootCertStore, ServerConfig};
1213
use serde::{Deserialize, Serialize};
1314
use tokio::net::{TcpListener, TcpSocket, TcpStream};
15+
use tokio_rustls::{TlsAcceptor, TlsConnector, TlsStream};
1416
use tracing::{error, info, warn};
1517

1618
/// A hook which listens for incoming connections and yields
@@ -49,12 +51,20 @@ impl QBHServerSocket {
4951

5052
impl QBHContext<QBIServerSocket> for QBHServerSocket {
5153
async fn run(self, init: QBHInit<QBIServerSocket>) {
54+
let root_cert_store = RootCertStore::empty();
55+
// TODO: add root certificate
56+
let config = ServerConfig::builder()
57+
.with_no_client_auth()
58+
.with_single_cert(todo!(), todo!())
59+
.unwrap();
60+
5261
loop {
5362
// listen on incoming connections
5463
let (stream, addr) = self.listener.accept().await.unwrap();
5564
info!("connected: {}", addr);
5665
// yield a [QBIServerSocket]
5766
init.attach(QBIServerSocket {
67+
config,
5868
stream,
5969
auth: self.auth.clone(),
6070
})
@@ -77,7 +87,17 @@ impl QBIContext for QBIClientSocket {
7787

7888
let socket = TcpSocket::new_v4().unwrap();
7989
let addr = self.addr.parse().unwrap();
80-
let mut stream = socket.connect(addr).await.unwrap();
90+
let stream = socket.connect(addr).await.unwrap();
91+
92+
let root_cert_store = RootCertStore::empty();
93+
// TODO: add root certificate
94+
let config = ClientConfig::builder()
95+
.with_root_certificates(root_cert_store)
96+
.with_no_client_auth();
97+
let connector = TlsConnector::from(Arc::new(config));
98+
let dnsname = ServerName::try_from("quixbyte.application").unwrap();
99+
let mut stream = connector.connect(dnsname, stream).await.unwrap();
100+
81101
let mut protocol = QBP::default();
82102
protocol.negotiate(&mut stream).await.unwrap();
83103
protocol
@@ -90,7 +110,7 @@ impl QBIContext for QBIClientSocket {
90110
let runner = Runner {
91111
host_id,
92112
com,
93-
stream,
113+
stream: TlsStream::Client(stream),
94114
protocol,
95115
};
96116

@@ -107,13 +127,18 @@ impl<'a> QBISetup<'a> for QBIClientSocket {
107127
#[derive(Debug)]
108128
pub struct QBIServerSocket {
109129
pub stream: TcpStream,
130+
pub config: ServerConfig,
110131
/// An authentication token sent on boot
111132
pub auth: Vec<u8>,
112133
}
113134

114135
impl QBIContext for QBIServerSocket {
115136
async fn run(self, host_id: QBDeviceId, com: QBIChannel) {
116-
let mut stream = self.stream;
137+
let stream = self.stream;
138+
139+
let acceptor = TlsAcceptor::from(Arc::new(self.config));
140+
let mut stream = acceptor.accept(stream).await.unwrap();
141+
117142
let mut protocol = QBP::default();
118143
protocol.negotiate(&mut stream).await.unwrap();
119144
let auth = protocol.recv_payload(&mut stream).await.unwrap();
@@ -125,7 +150,7 @@ impl QBIContext for QBIServerSocket {
125150
let runner = Runner {
126151
host_id,
127152
com,
128-
stream,
153+
stream: TlsStream::Server(stream),
129154
protocol,
130155
};
131156

@@ -136,7 +161,7 @@ impl QBIContext for QBIServerSocket {
136161
struct Runner {
137162
host_id: QBDeviceId,
138163
com: QBIChannel,
139-
stream: TcpStream,
164+
stream: TlsStream<TcpStream>,
140165
protocol: QBP,
141166
}
142167

0 commit comments

Comments
 (0)