blob: 43d037c5ffcb0864039d213cc13b5a3e05815e5c [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::{
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000021 error,
David Drysdale6c09af22023-11-06 09:57:10 +000022 key::MillisecondsSinceEpoch,
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000023 keyexchange,
David Drysdale6c09af22023-11-06 09:57:10 +000024 ta::{AuthGraphTa, Role},
25 traits,
26};
27use authgraph_hal::channel::SerializedChannel;
28use std::sync::{Arc, Mutex};
29use 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.)
34pub struct StdClock(Instant);
35
36impl Default for StdClock {
37 fn default() -> Self {
38 Self(Instant::now())
39 }
40}
41
42impl 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).
56pub struct LocalTa {
57 ta: Arc<Mutex<AuthGraphTa>>,
58}
59
60impl LocalTa {
61 /// Create a new instance.
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000062 pub fn new() -> Result<Self, error::Error> {
63 Ok(Self {
David Drysdale6c09af22023-11-06 09:57:10 +000064 ta: Arc::new(Mutex::new(AuthGraphTa::new(
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000065 keyexchange::AuthGraphParticipant::new(
66 boring::crypto_trait_impls(),
David Drysdale6c09af22023-11-06 09:57:10 +000067 Box::<boring::test_device::AgDevice>::default(),
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000068 keyexchange::MAX_OPENED_SESSIONS,
69 )?,
David Drysdale6c09af22023-11-06 09:57:10 +000070 Role::Both,
71 ))),
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000072 })
David Drysdale6c09af22023-11-06 09:57:10 +000073 }
74}
75
76/// Pretend to be a serialized channel to the TA, but actually just directly invoke the TA with
77/// incoming requests.
78impl 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}