Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Helper wrapper around RKPD interface. |
| 16 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 17 | use android_security_rkp_aidl::aidl::android::security::rkp::{ |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 18 | IGetKeyCallback::BnGetKeyCallback, IGetKeyCallback::ErrorCode::ErrorCode as GetKeyErrorCode, |
| 19 | IGetKeyCallback::IGetKeyCallback, IGetRegistrationCallback::BnGetRegistrationCallback, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 20 | IGetRegistrationCallback::IGetRegistrationCallback, IRegistration::IRegistration, |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 21 | IRemoteProvisioning::IRemoteProvisioning, |
| 22 | IStoreUpgradedKeyCallback::BnStoreUpgradedKeyCallback, |
| 23 | IStoreUpgradedKeyCallback::IStoreUpgradedKeyCallback, |
| 24 | RemotelyProvisionedKey::RemotelyProvisionedKey, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 25 | }; |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 26 | use anyhow::{Context, Result}; |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 27 | use binder::{BinderFeatures, Interface, StatusCode, Strong}; |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 28 | use message_macro::source_location_msg; |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 29 | use std::sync::Mutex; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 30 | use std::time::Duration; |
| 31 | use tokio::sync::oneshot; |
| 32 | use tokio::time::timeout; |
| 33 | |
| 34 | // Normally, we block indefinitely when making calls outside of keystore and rely on watchdog to |
| 35 | // report deadlocks. However, RKPD is mainline updatable. Also, calls to RKPD may wait on network |
| 36 | // for certificates. So, we err on the side of caution and timeout instead. |
| 37 | static RKPD_TIMEOUT: Duration = Duration::from_secs(10); |
| 38 | |
| 39 | fn tokio_rt() -> tokio::runtime::Runtime { |
| 40 | tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap() |
| 41 | } |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 42 | |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 43 | /// Errors occurred during the interaction with RKPD. |
| 44 | #[derive(Debug, Clone, Copy, thiserror::Error, PartialEq, Eq)] |
| 45 | pub enum Error { |
| 46 | /// An RKPD request gets cancelled. |
| 47 | #[error("An RKPD request gets cancelled")] |
| 48 | RequestCancelled, |
| 49 | |
| 50 | /// Failed to get registration. |
| 51 | #[error("Failed to get registration")] |
| 52 | GetRegistrationFailed, |
| 53 | |
| 54 | /// Failed to get key. |
| 55 | #[error("Failed to get key: {0:?}")] |
| 56 | GetKeyFailed(GetKeyErrorCode), |
| 57 | |
| 58 | /// Failed to store upgraded key. |
| 59 | #[error("Failed to store upgraded key")] |
| 60 | StoreUpgradedKeyFailed, |
| 61 | |
| 62 | /// Timeout when waiting for a callback. |
| 63 | #[error("Timeout when waiting for a callback")] |
| 64 | Timeout, |
| 65 | |
| 66 | /// Wraps a Binder status code. |
| 67 | #[error("Binder transaction error {0:?}")] |
| 68 | BinderTransaction(StatusCode), |
| 69 | } |
| 70 | |
| 71 | impl From<StatusCode> for Error { |
| 72 | fn from(s: StatusCode) -> Self { |
| 73 | Self::BinderTransaction(s) |
| 74 | } |
| 75 | } |
| 76 | |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 77 | /// Thread-safe channel for sending a value once and only once. If a value has |
| 78 | /// already been send, subsequent calls to send will noop. |
| 79 | struct SafeSender<T> { |
| 80 | inner: Mutex<Option<oneshot::Sender<T>>>, |
| 81 | } |
| 82 | |
| 83 | impl<T> SafeSender<T> { |
| 84 | fn new(sender: oneshot::Sender<T>) -> Self { |
| 85 | Self { inner: Mutex::new(Some(sender)) } |
| 86 | } |
| 87 | |
| 88 | fn send(&self, value: T) { |
| 89 | if let Some(inner) = self.inner.lock().unwrap().take() { |
Tri Vo | 0e5fe2c | 2023-02-15 17:02:06 -0800 | [diff] [blame] | 90 | // It's possible for the corresponding receiver to time out and be dropped. In this |
| 91 | // case send() will fail. This error is not actionable though, so only log the error. |
| 92 | if inner.send(value).is_err() { |
| 93 | log::error!("SafeSender::send() failed"); |
| 94 | } |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 98 | |
| 99 | struct GetRegistrationCallback { |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 100 | registration_tx: SafeSender<Result<binder::Strong<dyn IRegistration>>>, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | impl GetRegistrationCallback { |
| 104 | pub fn new_native_binder( |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 105 | registration_tx: oneshot::Sender<Result<binder::Strong<dyn IRegistration>>>, |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 106 | ) -> Strong<dyn IGetRegistrationCallback> { |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 107 | let result: Self = |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 108 | GetRegistrationCallback { registration_tx: SafeSender::new(registration_tx) }; |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 109 | BnGetRegistrationCallback::new_binder(result, BinderFeatures::default()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | impl Interface for GetRegistrationCallback {} |
| 114 | |
| 115 | impl IGetRegistrationCallback for GetRegistrationCallback { |
| 116 | fn onSuccess(&self, registration: &Strong<dyn IRegistration>) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 117 | self.registration_tx.send(Ok(registration.clone())); |
| 118 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 119 | } |
| 120 | fn onCancel(&self) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 121 | log::warn!("IGetRegistrationCallback cancelled"); |
| 122 | self.registration_tx.send( |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 123 | Err(Error::RequestCancelled) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 124 | .context(source_location_msg!("GetRegistrationCallback cancelled.")), |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 125 | ); |
| 126 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 127 | } |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 128 | fn onError(&self, description: &str) -> binder::Result<()> { |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 129 | log::error!("IGetRegistrationCallback failed: '{description}'"); |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 130 | self.registration_tx.send( |
| 131 | Err(Error::GetRegistrationFailed) |
| 132 | .context(source_location_msg!("GetRegistrationCallback failed: {:?}", description)), |
| 133 | ); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 134 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | /// Make a new connection to a IRegistration service. |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 139 | async fn get_rkpd_registration(rpc_name: &str) -> Result<binder::Strong<dyn IRegistration>> { |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 140 | let remote_provisioning: Strong<dyn IRemoteProvisioning> = |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 141 | binder::get_interface("remote_provisioning") |
| 142 | .map_err(Error::from) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 143 | .context(source_location_msg!("Trying to connect to IRemoteProvisioning service."))?; |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 144 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 145 | let (tx, rx) = oneshot::channel(); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 146 | let cb = GetRegistrationCallback::new_native_binder(tx); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 147 | |
| 148 | remote_provisioning |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 149 | .getRegistration(rpc_name, &cb) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 150 | .context(source_location_msg!("Trying to get registration."))?; |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 151 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 152 | match timeout(RKPD_TIMEOUT, rx).await { |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 153 | Err(e) => Err(Error::Timeout).context(source_location_msg!("Waiting for RKPD: {:?}", e)), |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 154 | Ok(v) => v.unwrap(), |
| 155 | } |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 158 | struct GetKeyCallback { |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 159 | key_tx: SafeSender<Result<RemotelyProvisionedKey>>, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | impl GetKeyCallback { |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 163 | pub fn new_native_binder( |
| 164 | key_tx: oneshot::Sender<Result<RemotelyProvisionedKey>>, |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 165 | ) -> Strong<dyn IGetKeyCallback> { |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 166 | let result: Self = GetKeyCallback { key_tx: SafeSender::new(key_tx) }; |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 167 | BnGetKeyCallback::new_binder(result, BinderFeatures::default()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | impl Interface for GetKeyCallback {} |
| 172 | |
| 173 | impl IGetKeyCallback for GetKeyCallback { |
| 174 | fn onSuccess(&self, key: &RemotelyProvisionedKey) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 175 | self.key_tx.send(Ok(RemotelyProvisionedKey { |
| 176 | keyBlob: key.keyBlob.clone(), |
| 177 | encodedCertChain: key.encodedCertChain.clone(), |
| 178 | })); |
| 179 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 180 | } |
| 181 | fn onCancel(&self) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 182 | log::warn!("IGetKeyCallback cancelled"); |
| 183 | self.key_tx.send( |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 184 | Err(Error::RequestCancelled).context(source_location_msg!("GetKeyCallback cancelled.")), |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 185 | ); |
| 186 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 187 | } |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 188 | fn onError(&self, error: GetKeyErrorCode, description: &str) -> binder::Result<()> { |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 189 | log::error!("IGetKeyCallback failed: {description}"); |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 190 | self.key_tx.send(Err(Error::GetKeyFailed(error)).context(source_location_msg!( |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 191 | "GetKeyCallback failed: {:?} {:?}", |
| 192 | error, |
| 193 | description |
| 194 | ))); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 195 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 199 | async fn get_rkpd_attestation_key_from_registration_async( |
| 200 | registration: &Strong<dyn IRegistration>, |
| 201 | caller_uid: u32, |
| 202 | ) -> Result<RemotelyProvisionedKey> { |
| 203 | let (tx, rx) = oneshot::channel(); |
| 204 | let cb = GetKeyCallback::new_native_binder(tx); |
| 205 | |
| 206 | registration |
| 207 | .getKey(caller_uid.try_into().unwrap(), &cb) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 208 | .context(source_location_msg!("Trying to get key."))?; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 209 | |
| 210 | match timeout(RKPD_TIMEOUT, rx).await { |
Tri Vo | 0e5fe2c | 2023-02-15 17:02:06 -0800 | [diff] [blame] | 211 | Err(e) => { |
| 212 | // Make a best effort attempt to cancel the timed out request. |
| 213 | if let Err(e) = registration.cancelGetKey(&cb) { |
| 214 | log::error!("IRegistration::cancelGetKey failed: {:?}", e); |
| 215 | } |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 216 | Err(Error::Timeout) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 217 | .context(source_location_msg!("Waiting for RKPD key timed out: {:?}", e)) |
Tri Vo | 0e5fe2c | 2023-02-15 17:02:06 -0800 | [diff] [blame] | 218 | } |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 219 | Ok(v) => v.unwrap(), |
| 220 | } |
| 221 | } |
| 222 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 223 | async fn get_rkpd_attestation_key_async( |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 224 | rpc_name: &str, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 225 | caller_uid: u32, |
| 226 | ) -> Result<RemotelyProvisionedKey> { |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 227 | let registration = get_rkpd_registration(rpc_name) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 228 | .await |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 229 | .context(source_location_msg!("Trying to get to IRegistration service."))?; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 230 | get_rkpd_attestation_key_from_registration_async(®istration, caller_uid).await |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 233 | struct StoreUpgradedKeyCallback { |
| 234 | completer: SafeSender<Result<()>>, |
| 235 | } |
| 236 | |
| 237 | impl StoreUpgradedKeyCallback { |
| 238 | pub fn new_native_binder( |
| 239 | completer: oneshot::Sender<Result<()>>, |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 240 | ) -> Strong<dyn IStoreUpgradedKeyCallback> { |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 241 | let result: Self = StoreUpgradedKeyCallback { completer: SafeSender::new(completer) }; |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 242 | BnStoreUpgradedKeyCallback::new_binder(result, BinderFeatures::default()) |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | impl Interface for StoreUpgradedKeyCallback {} |
| 247 | |
| 248 | impl IStoreUpgradedKeyCallback for StoreUpgradedKeyCallback { |
| 249 | fn onSuccess(&self) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 250 | self.completer.send(Ok(())); |
| 251 | Ok(()) |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | fn onError(&self, error: &str) -> binder::Result<()> { |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 255 | log::error!("IStoreUpgradedKeyCallback failed: {error}"); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 256 | self.completer.send( |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 257 | Err(Error::StoreUpgradedKeyFailed) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 258 | .context(source_location_msg!("Failed to store upgraded key: {:?}", error)), |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 259 | ); |
| 260 | Ok(()) |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 264 | async fn store_rkpd_attestation_key_with_registration_async( |
| 265 | registration: &Strong<dyn IRegistration>, |
| 266 | key_blob: &[u8], |
| 267 | upgraded_blob: &[u8], |
| 268 | ) -> Result<()> { |
| 269 | let (tx, rx) = oneshot::channel(); |
| 270 | let cb = StoreUpgradedKeyCallback::new_native_binder(tx); |
| 271 | |
| 272 | registration |
| 273 | .storeUpgradedKeyAsync(key_blob, upgraded_blob, &cb) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 274 | .context(source_location_msg!("Failed to store upgraded blob with RKPD."))?; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 275 | |
| 276 | match timeout(RKPD_TIMEOUT, rx).await { |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 277 | Err(e) => Err(Error::Timeout) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 278 | .context(source_location_msg!("Waiting for RKPD to complete storing key: {:?}", e)), |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 279 | Ok(v) => v.unwrap(), |
| 280 | } |
| 281 | } |
| 282 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 283 | async fn store_rkpd_attestation_key_async( |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 284 | rpc_name: &str, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 285 | key_blob: &[u8], |
| 286 | upgraded_blob: &[u8], |
| 287 | ) -> Result<()> { |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 288 | let registration = get_rkpd_registration(rpc_name) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 289 | .await |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 290 | .context(source_location_msg!("Trying to get to IRegistration service."))?; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 291 | store_rkpd_attestation_key_with_registration_async(®istration, key_blob, upgraded_blob).await |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /// Get attestation key from RKPD. |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 295 | pub fn get_rkpd_attestation_key(rpc_name: &str, caller_uid: u32) -> Result<RemotelyProvisionedKey> { |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 296 | tokio_rt().block_on(get_rkpd_attestation_key_async(rpc_name, caller_uid)) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /// Store attestation key in RKPD. |
| 300 | pub fn store_rkpd_attestation_key( |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 301 | rpc_name: &str, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 302 | key_blob: &[u8], |
| 303 | upgraded_blob: &[u8], |
| 304 | ) -> Result<()> { |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 305 | tokio_rt().block_on(store_rkpd_attestation_key_async(rpc_name, key_blob, upgraded_blob)) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | #[cfg(test)] |
| 309 | mod tests { |
| 310 | use super::*; |
| 311 | use android_security_rkp_aidl::aidl::android::security::rkp::IRegistration::BnRegistration; |
Tri Vo | 4b1cd82 | 2023-01-23 13:05:35 -0800 | [diff] [blame] | 312 | use std::sync::atomic::{AtomicU32, Ordering}; |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 313 | use std::sync::{Arc, Mutex}; |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 314 | |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 315 | const DEFAULT_RPC_SERVICE_NAME: &str = |
| 316 | "android.hardware.security.keymint.IRemotelyProvisionedComponent/default"; |
| 317 | |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 318 | struct MockRegistrationValues { |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 319 | key: RemotelyProvisionedKey, |
| 320 | latency: Option<Duration>, |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 321 | thread_join_handles: Vec<Option<std::thread::JoinHandle<()>>>, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 324 | struct MockRegistration(Arc<Mutex<MockRegistrationValues>>); |
| 325 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 326 | impl MockRegistration { |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 327 | pub fn new_native_binder( |
| 328 | key: &RemotelyProvisionedKey, |
| 329 | latency: Option<Duration>, |
| 330 | ) -> Strong<dyn IRegistration> { |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 331 | let result = Self(Arc::new(Mutex::new(MockRegistrationValues { |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 332 | key: RemotelyProvisionedKey { |
| 333 | keyBlob: key.keyBlob.clone(), |
| 334 | encodedCertChain: key.encodedCertChain.clone(), |
| 335 | }, |
| 336 | latency, |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 337 | thread_join_handles: Vec::new(), |
| 338 | }))); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 339 | BnRegistration::new_binder(result, BinderFeatures::default()) |
| 340 | } |
| 341 | } |
| 342 | |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 343 | impl Drop for MockRegistration { |
| 344 | fn drop(&mut self) { |
| 345 | let mut values = self.0.lock().unwrap(); |
| 346 | for handle in values.thread_join_handles.iter_mut() { |
| 347 | // These are test threads. So, no need to worry too much about error handling. |
| 348 | handle.take().unwrap().join().unwrap(); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 353 | impl Interface for MockRegistration {} |
| 354 | |
| 355 | impl IRegistration for MockRegistration { |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 356 | fn getKey(&self, _: i32, cb: &Strong<dyn IGetKeyCallback>) -> binder::Result<()> { |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 357 | let mut values = self.0.lock().unwrap(); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 358 | let key = RemotelyProvisionedKey { |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 359 | keyBlob: values.key.keyBlob.clone(), |
| 360 | encodedCertChain: values.key.encodedCertChain.clone(), |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 361 | }; |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 362 | let latency = values.latency; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 363 | let get_key_cb = cb.clone(); |
| 364 | |
| 365 | // Need a separate thread to trigger timeout in the caller. |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 366 | let join_handle = std::thread::spawn(move || { |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 367 | if let Some(duration) = latency { |
| 368 | std::thread::sleep(duration); |
| 369 | } |
| 370 | get_key_cb.onSuccess(&key).unwrap(); |
| 371 | }); |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 372 | values.thread_join_handles.push(Some(join_handle)); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 373 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | fn cancelGetKey(&self, _: &Strong<dyn IGetKeyCallback>) -> binder::Result<()> { |
Tri Vo | 0e5fe2c | 2023-02-15 17:02:06 -0800 | [diff] [blame] | 377 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 380 | fn storeUpgradedKeyAsync( |
| 381 | &self, |
| 382 | _: &[u8], |
| 383 | _: &[u8], |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 384 | cb: &Strong<dyn IStoreUpgradedKeyCallback>, |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 385 | ) -> binder::Result<()> { |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 386 | // We are primarily concerned with timing out correctly. Storing the key in this mock |
| 387 | // registration isn't particularly interesting, so skip that part. |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 388 | let values = self.0.lock().unwrap(); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 389 | let store_cb = cb.clone(); |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 390 | let latency = values.latency; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 391 | |
| 392 | std::thread::spawn(move || { |
| 393 | if let Some(duration) = latency { |
| 394 | std::thread::sleep(duration); |
| 395 | } |
| 396 | store_cb.onSuccess().unwrap(); |
| 397 | }); |
| 398 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 402 | fn get_mock_registration( |
| 403 | key: &RemotelyProvisionedKey, |
| 404 | latency: Option<Duration>, |
| 405 | ) -> Result<binder::Strong<dyn IRegistration>> { |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 406 | let (tx, rx) = oneshot::channel(); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 407 | let cb = GetRegistrationCallback::new_native_binder(tx); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 408 | let mock_registration = MockRegistration::new_native_binder(key, latency); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 409 | |
| 410 | assert!(cb.onSuccess(&mock_registration).is_ok()); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 411 | tokio_rt().block_on(rx).unwrap() |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Tri Vo | 4b1cd82 | 2023-01-23 13:05:35 -0800 | [diff] [blame] | 414 | // Using the same key ID makes test cases race with each other. So, we use separate key IDs for |
| 415 | // different test cases. |
| 416 | fn get_next_key_id() -> u32 { |
| 417 | static ID: AtomicU32 = AtomicU32::new(0); |
| 418 | ID.fetch_add(1, Ordering::Relaxed) |
| 419 | } |
| 420 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 421 | #[test] |
| 422 | fn test_get_registration_cb_success() { |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 423 | let key: RemotelyProvisionedKey = Default::default(); |
| 424 | let registration = get_mock_registration(&key, /*latency=*/ None); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 425 | assert!(registration.is_ok()); |
| 426 | } |
| 427 | |
| 428 | #[test] |
| 429 | fn test_get_registration_cb_cancel() { |
| 430 | let (tx, rx) = oneshot::channel(); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 431 | let cb = GetRegistrationCallback::new_native_binder(tx); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 432 | assert!(cb.onCancel().is_ok()); |
| 433 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 434 | let result = tokio_rt().block_on(rx).unwrap(); |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 435 | assert_eq!(result.unwrap_err().downcast::<Error>().unwrap(), Error::RequestCancelled); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | #[test] |
| 439 | fn test_get_registration_cb_error() { |
| 440 | let (tx, rx) = oneshot::channel(); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 441 | let cb = GetRegistrationCallback::new_native_binder(tx); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 442 | assert!(cb.onError("error").is_ok()); |
| 443 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 444 | let result = tokio_rt().block_on(rx).unwrap(); |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 445 | assert_eq!(result.unwrap_err().downcast::<Error>().unwrap(), Error::GetRegistrationFailed); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | #[test] |
| 449 | fn test_get_key_cb_success() { |
| 450 | let mock_key = |
| 451 | RemotelyProvisionedKey { keyBlob: vec![1, 2, 3], encodedCertChain: vec![4, 5, 6] }; |
| 452 | let (tx, rx) = oneshot::channel(); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 453 | let cb = GetKeyCallback::new_native_binder(tx); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 454 | assert!(cb.onSuccess(&mock_key).is_ok()); |
| 455 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 456 | let key = tokio_rt().block_on(rx).unwrap().unwrap(); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 457 | assert_eq!(key, mock_key); |
| 458 | } |
| 459 | |
| 460 | #[test] |
| 461 | fn test_get_key_cb_cancel() { |
| 462 | let (tx, rx) = oneshot::channel(); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 463 | let cb = GetKeyCallback::new_native_binder(tx); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 464 | assert!(cb.onCancel().is_ok()); |
| 465 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 466 | let result = tokio_rt().block_on(rx).unwrap(); |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 467 | assert_eq!(result.unwrap_err().downcast::<Error>().unwrap(), Error::RequestCancelled); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | #[test] |
| 471 | fn test_get_key_cb_error() { |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 472 | for get_key_error in GetKeyErrorCode::enum_values() { |
| 473 | let (tx, rx) = oneshot::channel(); |
| 474 | let cb = GetKeyCallback::new_native_binder(tx); |
| 475 | assert!(cb.onError(get_key_error, "error").is_ok()); |
| 476 | |
| 477 | let result = tokio_rt().block_on(rx).unwrap(); |
| 478 | assert_eq!( |
| 479 | result.unwrap_err().downcast::<Error>().unwrap(), |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 480 | Error::GetKeyFailed(get_key_error), |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 481 | ); |
| 482 | } |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | #[test] |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 486 | fn test_store_upgraded_cb_success() { |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 487 | let (tx, rx) = oneshot::channel(); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 488 | let cb = StoreUpgradedKeyCallback::new_native_binder(tx); |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 489 | assert!(cb.onSuccess().is_ok()); |
| 490 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 491 | tokio_rt().block_on(rx).unwrap().unwrap(); |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | #[test] |
| 495 | fn test_store_upgraded_key_cb_error() { |
| 496 | let (tx, rx) = oneshot::channel(); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 497 | let cb = StoreUpgradedKeyCallback::new_native_binder(tx); |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 498 | assert!(cb.onError("oh no! it failed").is_ok()); |
| 499 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 500 | let result = tokio_rt().block_on(rx).unwrap(); |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 501 | assert_eq!(result.unwrap_err().downcast::<Error>().unwrap(), Error::StoreUpgradedKeyFailed); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn test_get_mock_key_success() { |
| 506 | let mock_key = |
| 507 | RemotelyProvisionedKey { keyBlob: vec![1, 2, 3], encodedCertChain: vec![4, 5, 6] }; |
| 508 | let registration = get_mock_registration(&mock_key, /*latency=*/ None).unwrap(); |
| 509 | |
| 510 | let key = tokio_rt() |
| 511 | .block_on(get_rkpd_attestation_key_from_registration_async(®istration, 0)) |
| 512 | .unwrap(); |
| 513 | assert_eq!(key, mock_key); |
| 514 | } |
| 515 | |
| 516 | #[test] |
| 517 | fn test_get_mock_key_timeout() { |
| 518 | let mock_key = |
| 519 | RemotelyProvisionedKey { keyBlob: vec![1, 2, 3], encodedCertChain: vec![4, 5, 6] }; |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 520 | let latency = RKPD_TIMEOUT + Duration::from_secs(1); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 521 | let registration = get_mock_registration(&mock_key, Some(latency)).unwrap(); |
| 522 | |
| 523 | let result = |
| 524 | tokio_rt().block_on(get_rkpd_attestation_key_from_registration_async(®istration, 0)); |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 525 | assert_eq!(result.unwrap_err().downcast::<Error>().unwrap(), Error::Timeout); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | #[test] |
| 529 | fn test_store_mock_key_success() { |
| 530 | let mock_key = |
| 531 | RemotelyProvisionedKey { keyBlob: vec![1, 2, 3], encodedCertChain: vec![4, 5, 6] }; |
| 532 | let registration = get_mock_registration(&mock_key, /*latency=*/ None).unwrap(); |
| 533 | tokio_rt() |
| 534 | .block_on(store_rkpd_attestation_key_with_registration_async(®istration, &[], &[])) |
| 535 | .unwrap(); |
| 536 | } |
| 537 | |
| 538 | #[test] |
| 539 | fn test_store_mock_key_timeout() { |
| 540 | let mock_key = |
| 541 | RemotelyProvisionedKey { keyBlob: vec![1, 2, 3], encodedCertChain: vec![4, 5, 6] }; |
Tri Vo | 215f12e | 2023-02-15 16:23:39 -0800 | [diff] [blame] | 542 | let latency = RKPD_TIMEOUT + Duration::from_secs(1); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 543 | let registration = get_mock_registration(&mock_key, Some(latency)).unwrap(); |
| 544 | |
| 545 | let result = tokio_rt().block_on(store_rkpd_attestation_key_with_registration_async( |
| 546 | ®istration, |
| 547 | &[], |
| 548 | &[], |
| 549 | )); |
Alice Wang | 2dbabf3 | 2023-11-08 10:43:21 +0000 | [diff] [blame^] | 550 | assert_eq!(result.unwrap_err().downcast::<Error>().unwrap(), Error::Timeout); |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | #[test] |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 554 | fn test_get_rkpd_attestation_key() { |
Seth Moore | f896d36 | 2023-01-11 08:06:17 -0800 | [diff] [blame] | 555 | binder::ProcessState::start_thread_pool(); |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 556 | let key_id = get_next_key_id(); |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 557 | let key = get_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, key_id).unwrap(); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 558 | assert!(!key.keyBlob.is_empty()); |
| 559 | assert!(!key.encodedCertChain.is_empty()); |
| 560 | } |
| 561 | |
| 562 | #[test] |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 563 | fn test_get_rkpd_attestation_key_same_caller() { |
Seth Moore | f896d36 | 2023-01-11 08:06:17 -0800 | [diff] [blame] | 564 | binder::ProcessState::start_thread_pool(); |
Tri Vo | 4b1cd82 | 2023-01-23 13:05:35 -0800 | [diff] [blame] | 565 | let key_id = get_next_key_id(); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 566 | |
| 567 | // Multiple calls should return the same key. |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 568 | let first_key = get_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, key_id).unwrap(); |
| 569 | let second_key = get_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, key_id).unwrap(); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 570 | |
| 571 | assert_eq!(first_key.keyBlob, second_key.keyBlob); |
| 572 | assert_eq!(first_key.encodedCertChain, second_key.encodedCertChain); |
| 573 | } |
| 574 | |
| 575 | #[test] |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 576 | fn test_get_rkpd_attestation_key_different_caller() { |
Seth Moore | f896d36 | 2023-01-11 08:06:17 -0800 | [diff] [blame] | 577 | binder::ProcessState::start_thread_pool(); |
Tri Vo | 4b1cd82 | 2023-01-23 13:05:35 -0800 | [diff] [blame] | 578 | let first_key_id = get_next_key_id(); |
| 579 | let second_key_id = get_next_key_id(); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 580 | |
| 581 | // Different callers should be getting different keys. |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 582 | let first_key = get_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, first_key_id).unwrap(); |
| 583 | let second_key = get_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, second_key_id).unwrap(); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 584 | |
| 585 | assert_ne!(first_key.keyBlob, second_key.keyBlob); |
| 586 | assert_ne!(first_key.encodedCertChain, second_key.encodedCertChain); |
| 587 | } |
| 588 | |
| 589 | #[test] |
Tri Vo | bac3b52 | 2023-01-23 13:10:24 -0800 | [diff] [blame] | 590 | // Couple of things to note: |
| 591 | // 1. This test must never run with UID of keystore. Otherwise, it can mess up keys stored by |
| 592 | // keystore. |
| 593 | // 2. Storing and reading the stored key is prone to race condition. So, we only do this in one |
| 594 | // test case. |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 595 | fn test_store_rkpd_attestation_key() { |
Seth Moore | f896d36 | 2023-01-11 08:06:17 -0800 | [diff] [blame] | 596 | binder::ProcessState::start_thread_pool(); |
Tri Vo | 4b1cd82 | 2023-01-23 13:05:35 -0800 | [diff] [blame] | 597 | let key_id = get_next_key_id(); |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 598 | let key = get_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, key_id).unwrap(); |
Tri Vo | bac3b52 | 2023-01-23 13:10:24 -0800 | [diff] [blame] | 599 | let new_blob: [u8; 8] = rand::random(); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 600 | |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 601 | assert!( |
| 602 | store_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, &key.keyBlob, &new_blob).is_ok() |
| 603 | ); |
Tri Vo | bac3b52 | 2023-01-23 13:10:24 -0800 | [diff] [blame] | 604 | |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 605 | let new_key = get_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, key_id).unwrap(); |
Tri Vo | fc17949 | 2023-02-01 14:18:18 -0800 | [diff] [blame] | 606 | |
| 607 | // Restore original key so that we don't leave RKPD with invalid blobs. |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 608 | assert!( |
| 609 | store_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, &new_blob, &key.keyBlob).is_ok() |
| 610 | ); |
Tri Vo | bac3b52 | 2023-01-23 13:10:24 -0800 | [diff] [blame] | 611 | assert_eq!(new_key.keyBlob, new_blob); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 612 | } |
Tri Vo | 30268da | 2023-01-24 15:35:45 -0800 | [diff] [blame] | 613 | |
| 614 | #[test] |
Tri Vo | 02f0fa4 | 2023-01-24 12:32:08 -0800 | [diff] [blame] | 615 | fn test_stress_get_rkpd_attestation_key() { |
| 616 | binder::ProcessState::start_thread_pool(); |
| 617 | let key_id = get_next_key_id(); |
| 618 | let mut threads = vec![]; |
| 619 | const NTHREADS: u32 = 10; |
| 620 | const NCALLS: u32 = 1000; |
| 621 | |
| 622 | for _ in 0..NTHREADS { |
| 623 | threads.push(std::thread::spawn(move || { |
| 624 | for _ in 0..NCALLS { |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 625 | let key = get_rkpd_attestation_key(DEFAULT_RPC_SERVICE_NAME, key_id).unwrap(); |
Tri Vo | 02f0fa4 | 2023-01-24 12:32:08 -0800 | [diff] [blame] | 626 | assert!(!key.keyBlob.is_empty()); |
| 627 | assert!(!key.encodedCertChain.is_empty()); |
| 628 | } |
| 629 | })); |
| 630 | } |
| 631 | |
| 632 | for t in threads { |
| 633 | assert!(t.join().is_ok()); |
| 634 | } |
| 635 | } |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 636 | } |