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