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