blob: 1f851b28e8fe03e6ae86513d67ef99ea4c70853e [file] [log] [blame]
David Drysdale6c09af22023-11-06 09:57:10 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Common functionality for non-secure/testing instance of AuthGraph.
18
19use authgraph_boringssl as boring;
20use authgraph_core::{
David Drysdale20c65492023-11-23 15:49:54 +000021 error, keyexchange,
David Drysdale6c09af22023-11-06 09:57:10 +000022 ta::{AuthGraphTa, Role},
David Drysdale6c09af22023-11-06 09:57:10 +000023};
24use authgraph_hal::channel::SerializedChannel;
David Drysdale8898d2e2023-11-07 15:20:15 +000025use std::cell::RefCell;
26use std::rc::Rc;
27use std::sync::{mpsc, Mutex};
David Drysdale6c09af22023-11-06 09:57:10 +000028
29/// Implementation of the AuthGraph TA that runs locally in-process (and which is therefore
30/// insecure).
31pub struct LocalTa {
David Drysdale8898d2e2023-11-07 15:20:15 +000032 channels: Mutex<Channels>,
33}
34
35struct Channels {
36 in_tx: mpsc::Sender<Vec<u8>>,
37 out_rx: mpsc::Receiver<Vec<u8>>,
David Drysdale6c09af22023-11-06 09:57:10 +000038}
39
40impl LocalTa {
41 /// Create a new instance.
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000042 pub fn new() -> Result<Self, error::Error> {
David Drysdale8898d2e2023-11-07 15:20:15 +000043 // Create a pair of channels to communicate with the TA thread.
44 let (in_tx, in_rx) = mpsc::channel();
45 let (out_tx, out_rx) = mpsc::channel();
46
47 // The TA code expects to run single threaded, so spawn a thread to run it in.
48 std::thread::spawn(move || {
49 let mut ta = AuthGraphTa::new(
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000050 keyexchange::AuthGraphParticipant::new(
51 boring::crypto_trait_impls(),
David Drysdale8898d2e2023-11-07 15:20:15 +000052 Rc::new(RefCell::new(boring::test_device::AgDevice::default())),
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000053 keyexchange::MAX_OPENED_SESSIONS,
David Drysdale8898d2e2023-11-07 15:20:15 +000054 )
55 .expect("failed to create AG participant"),
David Drysdale6c09af22023-11-06 09:57:10 +000056 Role::Both,
David Drysdale8898d2e2023-11-07 15:20:15 +000057 );
58 // Loop forever processing request messages.
59 loop {
60 let req_data: Vec<u8> = in_rx.recv().expect("failed to receive next req");
61 let rsp_data = ta.process(&req_data);
62 out_tx.send(rsp_data).expect("failed to send out rsp");
63 }
64 });
65 Ok(Self {
66 channels: Mutex::new(Channels { in_tx, out_rx }),
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000067 })
David Drysdale6c09af22023-11-06 09:57:10 +000068 }
69}
70
David Drysdale6c09af22023-11-06 09:57:10 +000071impl SerializedChannel for LocalTa {
72 const MAX_SIZE: usize = usize::MAX;
73
David Drysdale8898d2e2023-11-07 15:20:15 +000074 fn execute(&self, req_data: &[u8]) -> binder::Result<Vec<u8>> {
75 // Serialize across both request and response.
76 let channels = self.channels.lock().unwrap();
77 channels
78 .in_tx
79 .send(req_data.to_vec())
80 .expect("failed to send in request");
81 Ok(channels.out_rx.recv().expect("failed to receive response"))
David Drysdale6c09af22023-11-06 09:57:10 +000082 }
83}