Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +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 | |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 17 | //! Non-secure implementation of the Secretkeeper HAL. |
| 18 | |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 19 | use log::{error, info, Level}; |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 20 | use std::sync::{Arc, Mutex}; |
| 21 | use authgraph_boringssl as boring; |
| 22 | use authgraph_core::ta::{Role, AuthGraphTa}; |
| 23 | use authgraph_core::keyexchange::{MAX_OPENED_SESSIONS, AuthGraphParticipant}; |
| 24 | use secretkeeper_comm::ta::SecretkeeperTa; |
| 25 | use secretkeeper_hal::SecretkeeperService; |
| 26 | use authgraph_hal::channel::SerializedChannel; |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 27 | use android_hardware_security_secretkeeper::aidl::android::hardware::security::secretkeeper::ISecretkeeper::{ |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 28 | ISecretkeeper, BpSecretkeeper, |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 29 | }; |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 30 | use std::cell::RefCell; |
| 31 | use std::rc::Rc; |
| 32 | use std::sync::mpsc; |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 33 | |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 34 | /// Implementation of the Secrekeeper TA that runs locally in-process (and which is therefore |
| 35 | /// insecure). |
| 36 | pub struct LocalTa { |
| 37 | in_tx: mpsc::Sender<Vec<u8>>, |
| 38 | out_rx: mpsc::Receiver<Vec<u8>>, |
| 39 | } |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 40 | |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 41 | /// Prefix byte for messages intended for the AuthGraph TA. |
| 42 | const AG_MESSAGE_PREFIX: u8 = 0x00; |
| 43 | /// Prefix byte for messages intended for the Secretkeeper TA. |
| 44 | const SK_MESSAGE_PREFIX: u8 = 0x01; |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 45 | |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 46 | impl LocalTa { |
| 47 | /// Create a new instance. |
| 48 | pub fn new() -> Self { |
| 49 | // Create a pair of channels to communicate with the TA thread. |
| 50 | let (in_tx, in_rx) = mpsc::channel(); |
| 51 | let (out_tx, out_rx) = mpsc::channel(); |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 52 | |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 53 | // The TA code expects to run single threaded, so spawn a thread to run it in. |
| 54 | std::thread::spawn(move || { |
| 55 | let mut crypto_impls = boring::crypto_trait_impls(); |
| 56 | let sk_ta = Rc::new(RefCell::new( |
| 57 | SecretkeeperTa::new(&mut crypto_impls) |
| 58 | .expect("Failed to create local Secretkeeper TA"), |
| 59 | )); |
| 60 | let mut ag_ta = AuthGraphTa::new( |
| 61 | AuthGraphParticipant::new(crypto_impls, sk_ta.clone(), MAX_OPENED_SESSIONS) |
| 62 | .expect("Failed to create local AuthGraph TA"), |
| 63 | Role::Sink, |
| 64 | ); |
| 65 | |
| 66 | // Loop forever processing request messages. |
| 67 | loop { |
| 68 | let req_data: Vec<u8> = in_rx.recv().expect("failed to receive next req"); |
| 69 | let rsp_data = match req_data[0] { |
| 70 | AG_MESSAGE_PREFIX => ag_ta.process(&req_data[1..]), |
| 71 | SK_MESSAGE_PREFIX => { |
| 72 | // It's safe to `borrow_mut()` because this code is not a callback |
| 73 | // from AuthGraph (the only other holder of an `Rc`), and so there |
| 74 | // can be no live `borrow()`s in this (single) thread. |
| 75 | sk_ta.borrow_mut().process(&req_data[1..]) |
| 76 | } |
| 77 | prefix => panic!("unexpected messageprefix {prefix}!"), |
| 78 | }; |
| 79 | out_tx.send(rsp_data).expect("failed to send out rsp"); |
| 80 | } |
| 81 | }); |
| 82 | Self { in_tx, out_rx } |
| 83 | } |
| 84 | |
| 85 | fn execute_for(&mut self, prefix: u8, req_data: &[u8]) -> Vec<u8> { |
| 86 | let mut prefixed_req = Vec::with_capacity(req_data.len() + 1); |
| 87 | prefixed_req.push(prefix); |
| 88 | prefixed_req.extend_from_slice(req_data); |
| 89 | self.in_tx |
| 90 | .send(prefixed_req) |
| 91 | .expect("failed to send in request"); |
| 92 | self.out_rx.recv().expect("failed to receive response") |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 96 | pub struct AuthGraphChannel(Arc<Mutex<LocalTa>>); |
| 97 | impl SerializedChannel for AuthGraphChannel { |
| 98 | const MAX_SIZE: usize = usize::MAX; |
| 99 | fn execute(&self, req_data: &[u8]) -> binder::Result<Vec<u8>> { |
| 100 | Ok(self |
| 101 | .0 |
| 102 | .lock() |
| 103 | .unwrap() |
| 104 | .execute_for(AG_MESSAGE_PREFIX, req_data)) |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 105 | } |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 106 | } |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 107 | |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 108 | pub struct SecretkeeperChannel(Arc<Mutex<LocalTa>>); |
| 109 | impl SerializedChannel for SecretkeeperChannel { |
| 110 | const MAX_SIZE: usize = usize::MAX; |
| 111 | fn execute(&self, req_data: &[u8]) -> binder::Result<Vec<u8>> { |
| 112 | Ok(self |
| 113 | .0 |
| 114 | .lock() |
| 115 | .unwrap() |
| 116 | .execute_for(SK_MESSAGE_PREFIX, req_data)) |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | fn main() { |
| 121 | // Initialize Android logging. |
| 122 | android_logger::init_once( |
| 123 | android_logger::Config::default() |
| 124 | .with_tag("NonSecureSecretkeeper") |
| 125 | .with_min_level(Level::Info) |
| 126 | .with_log_id(android_logger::LogId::System), |
| 127 | ); |
| 128 | // Redirect panic messages to logcat. |
| 129 | std::panic::set_hook(Box::new(|panic_info| { |
| 130 | error!("{}", panic_info); |
| 131 | })); |
| 132 | |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 133 | let ta = Arc::new(Mutex::new(LocalTa::new())); |
| 134 | let ag_channel = AuthGraphChannel(ta.clone()); |
| 135 | let sk_channel = SecretkeeperChannel(ta.clone()); |
| 136 | |
| 137 | let service = SecretkeeperService::new_as_binder(sk_channel, ag_channel); |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 138 | let service_name = format!( |
| 139 | "{}/nonsecure", |
| 140 | <BpSecretkeeper as ISecretkeeper>::get_descriptor() |
| 141 | ); |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame^] | 142 | binder::add_service(&service_name, service.as_binder()).unwrap_or_else(|e| { |
| 143 | panic!("Failed to register service {service_name} because of {e:?}.",); |
Shikha Panwar | eb223ba | 2023-10-19 14:54:06 +0000 | [diff] [blame] | 144 | }); |
| 145 | info!("Registered Binder service, joining threadpool."); |
| 146 | binder::ProcessState::join_thread_pool(); |
| 147 | } |