1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
1415#[cfg(test)]
16use matrix_sdk_base::crypto::types::qr_login::QrCodeModeData;
17use matrix_sdk_base::crypto::types::qr_login::{QrCodeData, QrCodeMode};
18use serde::{de::DeserializeOwned, Serialize};
19use tracing::{instrument, trace};
20#[cfg(test)]
21use url::Url;
22use vodozemac::ecies::{CheckCode, Ecies, EstablishedEcies, Message, OutboundCreationResult};
23#[cfg(test)]
24use vodozemac::ecies::{InboundCreationResult, InitialMessage};
2526use super::{
27 rendezvous_channel::{InboundChannelCreationResult, RendezvousChannel},
28 SecureChannelError as Error,
29};
30use crate::{config::RequestConfig, http_client::HttpClient};
3132const LOGIN_INITIATE_MESSAGE: &str = "MATRIX_QR_CODE_LOGIN_INITIATE";
33const LOGIN_OK_MESSAGE: &str = "MATRIX_QR_CODE_LOGIN_OK";
3435#[cfg(test)]
36pub(super) struct SecureChannel {
37 channel: RendezvousChannel,
38 qr_code_data: QrCodeData,
39 ecies: Ecies,
40}
4142// This is only used in tests because we're only supporting the new device part
43// of the QR login flow. It will be needed once we support reciprocating of the
44// login.
45//
46// It's still very much useful to have this, as we're testing the whole flow by
47// mocking the reciprocation.
48#[cfg(test)]
49impl SecureChannel {
50pub(super) async fn new(http_client: HttpClient, homeserver_url: &Url) -> Result<Self, Error> {
51let channel = RendezvousChannel::create_outbound(http_client, homeserver_url).await?;
52let rendezvous_url = channel.rendezvous_url().to_owned();
53// We're a bit abusing the QR code data here, since we're passing the homeserver
54 // URL, but for our tests this is fine.
55let mode_data = QrCodeModeData::Reciprocate { server_name: homeserver_url.to_string() };
5657let ecies = Ecies::new();
58let public_key = ecies.public_key();
5960let qr_code_data = QrCodeData { public_key, rendezvous_url, mode_data };
6162Ok(Self { channel, qr_code_data, ecies })
63 }
6465pub(super) fn qr_code_data(&self) -> &QrCodeData {
66&self.qr_code_data
67 }
6869#[instrument(skip(self))]
70pub(super) async fn connect(mut self) -> Result<AlmostEstablishedSecureChannel, Error> {
71trace!("Trying to connect the secure channel.");
7273let message = self.channel.receive().await?;
74let message = std::str::from_utf8(&message)?;
75let message = InitialMessage::decode(message)?;
7677let InboundCreationResult { ecies, message } =
78self.ecies.establish_inbound_channel(&message)?;
79let message = std::str::from_utf8(&message)?;
8081trace!("Received the initial secure channel message");
8283if message == LOGIN_INITIATE_MESSAGE {
84let mut secure_channel = EstablishedSecureChannel { channel: self.channel, ecies };
8586trace!("Sending the LOGIN OK message");
8788 secure_channel.send(LOGIN_OK_MESSAGE).await?;
8990Ok(AlmostEstablishedSecureChannel { secure_channel })
91 } else {
92Err(Error::SecureChannelMessage {
93 expected: LOGIN_INITIATE_MESSAGE,
94 received: message.to_owned(),
95 })
96 }
97 }
98}
99100/// An SecureChannel that is yet to be confirmed as with the [`CheckCode`].
101/// Same deal as for the [`SecureChannel`], not used for now.
102#[cfg(test)]
103pub(super) struct AlmostEstablishedSecureChannel {
104 secure_channel: EstablishedSecureChannel,
105}
106107#[cfg(test)]
108impl AlmostEstablishedSecureChannel {
109/// Confirm that the secure channel is indeed secure.
110 ///
111 /// The check code needs to be received out of band from the other side of
112 /// the secure channel.
113pub(super) fn confirm(self, check_code: u8) -> Result<EstablishedSecureChannel, Error> {
114if check_code == self.secure_channel.check_code().to_digit() {
115Ok(self.secure_channel)
116 } else {
117Err(Error::InvalidCheckCode)
118 }
119 }
120}
121122pub(super) struct EstablishedSecureChannel {
123 channel: RendezvousChannel,
124 ecies: EstablishedEcies,
125}
126127impl EstablishedSecureChannel {
128/// Establish a secure channel from a scanned QR code.
129#[instrument(skip(client))]
130pub(super) async fn from_qr_code(
131 client: reqwest::Client,
132 qr_code_data: &QrCodeData,
133 expected_mode: QrCodeMode,
134 ) -> Result<Self, Error> {
135if qr_code_data.mode() == expected_mode {
136Err(Error::InvalidIntent)
137 } else {
138trace!("Attempting to create a new inbound secure channel from a QR code.");
139140let client = HttpClient::new(client, RequestConfig::short_retry());
141let ecies = Ecies::new();
142143// Let's establish an outbound ECIES channel, the other side won't know that
144 // it's talking to us, the device that scanned the QR code, until it
145 // receives and successfully decrypts the initial message. We're here encrypting
146 // the `LOGIN_INITIATE_MESSAGE`.
147let OutboundCreationResult { ecies, message } = ecies.establish_outbound_channel(
148 qr_code_data.public_key,
149 LOGIN_INITIATE_MESSAGE.as_bytes(),
150 )?;
151152// The other side has crated a rendezvous channel, we're going to connect to it
153 // and send this initial encrypted message through it. The initial message on
154 // the rendezvous channel will have an empty body, so we can just
155 // drop it.
156let InboundChannelCreationResult { mut channel, .. } =
157 RendezvousChannel::create_inbound(client, &qr_code_data.rendezvous_url).await?;
158159trace!(
160"Received the initial message from the rendezvous channel, sending the LOGIN \
161 INITIATE message"
162);
163164// Now we're sending the encrypted message through the rendezvous channel to the
165 // other side.
166let encoded_message = message.encode().as_bytes().to_vec();
167 channel.send(encoded_message).await?;
168169trace!("Waiting for the LOGIN OK message");
170171// We can create our EstablishedSecureChannel struct now and use the
172 // convenient helpers which transparently decrypt on receival.
173let mut ret = Self { channel, ecies };
174let response = ret.receive().await?;
175176trace!("Received the LOGIN OK message, maybe.");
177178if response == LOGIN_OK_MESSAGE {
179Ok(ret)
180 } else {
181Err(Error::SecureChannelMessage { expected: LOGIN_OK_MESSAGE, received: response })
182 }
183 }
184 }
185186/// Get the [`CheckCode`] which can be used to, out of band, verify that
187 /// both sides of the channel are indeed communicating with each other and
188 /// not with a 3rd party.
189pub(super) fn check_code(&self) -> &CheckCode {
190self.ecies.check_code()
191 }
192193/// Send the given message over to the other side.
194 ///
195 /// The message will be encrypted before it is sent over the rendezvous
196 /// channel.
197pub(super) async fn send_json(&mut self, message: impl Serialize) -> Result<(), Error> {
198let message = serde_json::to_string(&message)?;
199self.send(&message).await
200}
201202/// Attempt to receive a message from the channel.
203 ///
204 /// The message will be decrypted after it has been received over the
205 /// rendezvous channel.
206pub(super) async fn receive_json<D: DeserializeOwned>(&mut self) -> Result<D, Error> {
207let message = self.receive().await?;
208Ok(serde_json::from_str(&message)?)
209 }
210211async fn send(&mut self, message: &str) -> Result<(), Error> {
212let message = self.ecies.encrypt(message.as_bytes());
213let message = message.encode();
214215Ok(self.channel.send(message.as_bytes().to_vec()).await?)
216 }
217218async fn receive(&mut self) -> Result<String, Error> {
219let message = self.channel.receive().await?;
220let ciphertext = std::str::from_utf8(&message)?;
221let message = Message::decode(ciphertext)?;
222223let decrypted = self.ecies.decrypt(&message)?;
224225Ok(String::from_utf8(decrypted).map_err(|e| e.utf8_error())?)
226 }
227}
228229#[cfg(test)]
230pub(super) mod test {
231use std::sync::{
232 atomic::{AtomicU8, Ordering},
233 Arc, Mutex,
234 };
235236use matrix_sdk_base::crypto::types::qr_login::QrCodeMode;
237use matrix_sdk_test::async_test;
238use serde_json::json;
239use similar_asserts::assert_eq;
240use url::Url;
241use wiremock::{
242 matchers::{method, path},
243 Mock, MockGuard, MockServer, ResponseTemplate,
244 };
245246use super::{EstablishedSecureChannel, SecureChannel};
247use crate::http_client::HttpClient;
248249#[allow(dead_code)]
250pub struct MockedRendezvousServer {
251pub homeserver_url: Url,
252pub rendezvous_url: Url,
253 content: Arc<Mutex<Option<String>>>,
254 etag: Arc<AtomicU8>,
255 post_guard: MockGuard,
256 put_guard: MockGuard,
257 get_guard: MockGuard,
258 }
259260impl MockedRendezvousServer {
261pub async fn new(server: &MockServer, location: &str) -> Self {
262let content: Arc<Mutex<Option<String>>> = Mutex::default().into();
263let etag = Arc::new(AtomicU8::new(0));
264265let homeserver_url = Url::parse(&server.uri())
266 .expect("We should be able to parse the example homeserver");
267268let rendezvous_url = homeserver_url
269 .join(location)
270 .expect("We should be able to create a rendezvous URL");
271272let post_guard = server
273 .register_as_scoped(
274 Mock::given(method("POST"))
275 .and(path("/_matrix/client/unstable/org.matrix.msc4108/rendezvous"))
276 .respond_with(
277 ResponseTemplate::new(200)
278 .append_header("X-Max-Bytes", "10240")
279 .append_header("ETag", "1")
280 .append_header("Expires", "Wed, 07 Sep 2022 14:28:51 GMT")
281 .append_header("Last-Modified", "Wed, 07 Sep 2022 14:27:51 GMT")
282 .set_body_json(json!({
283"url": rendezvous_url,
284 })),
285 ),
286 )
287 .await;
288289let put_guard = server
290 .register_as_scoped(
291 Mock::given(method("PUT")).and(path("/abcdEFG12345")).respond_with({
292let content = content.clone();
293let etag = etag.clone();
294295move |request: &wiremock::Request| {
296*content.lock().unwrap() =
297Some(String::from_utf8(request.body.clone()).unwrap());
298let current_etag = etag.fetch_add(1, Ordering::SeqCst);
299300 ResponseTemplate::new(200)
301 .append_header("ETag", (current_etag + 2).to_string())
302 .append_header("Expires", "Wed, 07 Sep 2022 14:28:51 GMT")
303 .append_header("Last-Modified", "Wed, 07 Sep 2022 14:27:51 GMT")
304 }
305 }),
306 )
307 .await;
308309let get_guard = server
310 .register_as_scoped(
311 Mock::given(method("GET")).and(path("/abcdEFG12345")).respond_with({
312let content = content.clone();
313let etag = etag.clone();
314315move |request: &wiremock::Request| {
316let requested_etag = request.headers.get("if-none-match").map(|etag| {
317 str::parse::<u8>(std::str::from_utf8(etag.as_bytes()).unwrap())
318 .unwrap()
319 });
320321let mut content = content.lock().unwrap();
322let current_etag = etag.load(Ordering::SeqCst);
323324if requested_etag == Some(current_etag) || requested_etag.is_none() {
325let content = content.take();
326327 ResponseTemplate::new(200)
328 .append_header("ETag", (current_etag).to_string())
329 .append_header("Expires", "Wed, 07 Sep 2022 14:28:51 GMT")
330 .append_header("Last-Modified", "Wed, 07 Sep 2022 14:27:51 GMT")
331 .set_body_string(content.unwrap_or_default())
332 } else {
333let etag = requested_etag.unwrap_or_default();
334335 ResponseTemplate::new(304)
336 .append_header("ETag", etag.to_string())
337 .append_header("Expires", "Wed, 07 Sep 2022 14:28:51 GMT")
338 .append_header("Last-Modified", "Wed, 07 Sep 2022 14:27:51 GMT")
339 }
340 }
341 }),
342 )
343 .await;
344345Self { content, etag, post_guard, put_guard, get_guard, homeserver_url, rendezvous_url }
346 }
347 }
348349#[async_test]
350async fn test_creation() {
351let server = MockServer::start().await;
352let rendezvous_server = MockedRendezvousServer::new(&server, "abcdEFG12345").await;
353354let client = HttpClient::new(reqwest::Client::new(), Default::default());
355let alice = SecureChannel::new(client, &rendezvous_server.homeserver_url)
356 .await
357.expect("Alice should be able to create a secure channel.");
358359let qr_code_data = alice.qr_code_data().clone();
360361let bob_task = tokio::spawn(async move {
362 EstablishedSecureChannel::from_qr_code(
363 reqwest::Client::new(),
364&qr_code_data,
365 QrCodeMode::Login,
366 )
367 .await
368.expect("Bob should be able to fully establish the secure channel.")
369 });
370371let alice_task = tokio::spawn(async move {
372 alice
373 .connect()
374 .await
375.expect("Alice should be able to connect the established secure channel")
376 });
377378let bob = bob_task.await.unwrap();
379let alice = alice_task.await.unwrap();
380381assert_eq!(alice.secure_channel.check_code(), bob.check_code());
382383let alice = alice
384 .confirm(bob.check_code().to_digit())
385 .expect("Alice should be able to confirm the established secure channel.");
386387assert_eq!(bob.channel.rendezvous_url(), alice.channel.rendezvous_url());
388 }
389}