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