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