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