blob: 14741aafeed7f379848b6768e3f4e9f7cd43ff9e [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;
25use std::sync::{Arc, Mutex};
David Drysdale6c09af22023-11-06 09:57:10 +000026
27/// Implementation of the AuthGraph TA that runs locally in-process (and which is therefore
28/// insecure).
29pub struct LocalTa {
30 ta: Arc<Mutex<AuthGraphTa>>,
31}
32
33impl LocalTa {
34 /// Create a new instance.
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000035 pub fn new() -> Result<Self, error::Error> {
36 Ok(Self {
David Drysdale6c09af22023-11-06 09:57:10 +000037 ta: Arc::new(Mutex::new(AuthGraphTa::new(
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000038 keyexchange::AuthGraphParticipant::new(
39 boring::crypto_trait_impls(),
David Drysdale6c09af22023-11-06 09:57:10 +000040 Box::<boring::test_device::AgDevice>::default(),
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000041 keyexchange::MAX_OPENED_SESSIONS,
42 )?,
David Drysdale6c09af22023-11-06 09:57:10 +000043 Role::Both,
44 ))),
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000045 })
David Drysdale6c09af22023-11-06 09:57:10 +000046 }
47}
48
49/// Pretend to be a serialized channel to the TA, but actually just directly invoke the TA with
50/// incoming requests.
51impl SerializedChannel for LocalTa {
52 const MAX_SIZE: usize = usize::MAX;
53
54 fn execute(&mut self, req_data: &[u8]) -> binder::Result<Vec<u8>> {
55 Ok(self.ta.lock().unwrap().process(req_data))
56 }
57}