David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | use authgraph_boringssl as boring; |
| 20 | use authgraph_core::{ |
Hasini Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 21 | error, |
David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 22 | key::MillisecondsSinceEpoch, |
Hasini Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 23 | keyexchange, |
David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 24 | ta::{AuthGraphTa, Role}, |
| 25 | traits, |
| 26 | }; |
| 27 | use authgraph_hal::channel::SerializedChannel; |
| 28 | use std::sync::{Arc, Mutex}; |
| 29 | use std::time::Instant; |
| 30 | |
| 31 | /// Monotonic clock with an epoch that starts at the point of construction. |
| 32 | /// (This makes it unsuitable for use outside of testing, because the epoch |
| 33 | /// will not match that of any other component.) |
| 34 | pub struct StdClock(Instant); |
| 35 | |
| 36 | impl Default for StdClock { |
| 37 | fn default() -> Self { |
| 38 | Self(Instant::now()) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl traits::MonotonicClock for StdClock { |
| 43 | fn now(&self) -> MillisecondsSinceEpoch { |
| 44 | let millis: i64 = self |
| 45 | .0 |
| 46 | .elapsed() |
| 47 | .as_millis() |
| 48 | .try_into() |
| 49 | .expect("failed to fit timestamp in i64"); |
| 50 | MillisecondsSinceEpoch(millis) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Implementation of the AuthGraph TA that runs locally in-process (and which is therefore |
| 55 | /// insecure). |
| 56 | pub struct LocalTa { |
| 57 | ta: Arc<Mutex<AuthGraphTa>>, |
| 58 | } |
| 59 | |
| 60 | impl LocalTa { |
| 61 | /// Create a new instance. |
Hasini Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 62 | pub fn new() -> Result<Self, error::Error> { |
| 63 | Ok(Self { |
David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 64 | ta: Arc::new(Mutex::new(AuthGraphTa::new( |
Hasini Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 65 | keyexchange::AuthGraphParticipant::new( |
| 66 | boring::crypto_trait_impls(), |
David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 67 | Box::<boring::test_device::AgDevice>::default(), |
Hasini Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 68 | keyexchange::MAX_OPENED_SESSIONS, |
| 69 | )?, |
David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 70 | Role::Both, |
| 71 | ))), |
Hasini Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 72 | }) |
David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | /// Pretend to be a serialized channel to the TA, but actually just directly invoke the TA with |
| 77 | /// incoming requests. |
| 78 | impl SerializedChannel for LocalTa { |
| 79 | const MAX_SIZE: usize = usize::MAX; |
| 80 | |
| 81 | fn execute(&mut self, req_data: &[u8]) -> binder::Result<Vec<u8>> { |
| 82 | Ok(self.ta.lock().unwrap().process(req_data)) |
| 83 | } |
| 84 | } |