blob: 1d6ffb3108acaa582045657c8eddca8e521c4115 [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 Drysdaleb108e8c2023-12-18 17:22:15 +000025use log::error;
David Drysdale8898d2e2023-11-07 15:20:15 +000026use std::cell::RefCell;
27use std::rc::Rc;
28use std::sync::{mpsc, Mutex};
David Drysdale6c09af22023-11-06 09:57:10 +000029
30/// Implementation of the AuthGraph TA that runs locally in-process (and which is therefore
31/// insecure).
32pub struct LocalTa {
David Drysdale8898d2e2023-11-07 15:20:15 +000033 channels: Mutex<Channels>,
34}
35
36struct Channels {
37 in_tx: mpsc::Sender<Vec<u8>>,
38 out_rx: mpsc::Receiver<Vec<u8>>,
David Drysdale6c09af22023-11-06 09:57:10 +000039}
40
41impl LocalTa {
42 /// Create a new instance.
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000043 pub fn new() -> Result<Self, error::Error> {
David Drysdale8898d2e2023-11-07 15:20:15 +000044 // Create a pair of channels to communicate with the TA thread.
45 let (in_tx, in_rx) = mpsc::channel();
46 let (out_tx, out_rx) = mpsc::channel();
47
48 // The TA code expects to run single threaded, so spawn a thread to run it in.
49 std::thread::spawn(move || {
50 let mut ta = AuthGraphTa::new(
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000051 keyexchange::AuthGraphParticipant::new(
52 boring::crypto_trait_impls(),
David Drysdale8898d2e2023-11-07 15:20:15 +000053 Rc::new(RefCell::new(boring::test_device::AgDevice::default())),
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000054 keyexchange::MAX_OPENED_SESSIONS,
David Drysdale8898d2e2023-11-07 15:20:15 +000055 )
56 .expect("failed to create AG participant"),
David Drysdale6c09af22023-11-06 09:57:10 +000057 Role::Both,
David Drysdale8898d2e2023-11-07 15:20:15 +000058 );
59 // Loop forever processing request messages.
60 loop {
David Drysdaleb108e8c2023-12-18 17:22:15 +000061 let req_data: Vec<u8> = match in_rx.recv() {
62 Ok(data) => data,
63 Err(_) => {
64 error!("local TA failed to receive request!");
65 break;
66 }
67 };
David Drysdale8898d2e2023-11-07 15:20:15 +000068 let rsp_data = ta.process(&req_data);
David Drysdaleb108e8c2023-12-18 17:22:15 +000069 match out_tx.send(rsp_data) {
70 Ok(_) => {}
71 Err(_) => {
72 error!("local TA failed to send out response");
73 break;
74 }
75 }
David Drysdale8898d2e2023-11-07 15:20:15 +000076 }
David Drysdaleb108e8c2023-12-18 17:22:15 +000077 error!("local TA terminating!");
David Drysdale8898d2e2023-11-07 15:20:15 +000078 });
79 Ok(Self {
80 channels: Mutex::new(Channels { in_tx, out_rx }),
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000081 })
David Drysdale6c09af22023-11-06 09:57:10 +000082 }
83}
84
David Drysdale6c09af22023-11-06 09:57:10 +000085impl SerializedChannel for LocalTa {
86 const MAX_SIZE: usize = usize::MAX;
87
David Drysdale8898d2e2023-11-07 15:20:15 +000088 fn execute(&self, req_data: &[u8]) -> binder::Result<Vec<u8>> {
89 // Serialize across both request and response.
90 let channels = self.channels.lock().unwrap();
91 channels
92 .in_tx
93 .send(req_data.to_vec())
94 .expect("failed to send in request");
95 Ok(channels.out_rx.recv().expect("failed to receive response"))
David Drysdale6c09af22023-11-06 09:57:10 +000096 }
97}