Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Helper wrapper around RKPD interface. |
| 16 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 17 | use android_security_rkp_aidl::aidl::android::security::rkp::{ |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 18 | IGetKeyCallback::BnGetKeyCallback, IGetKeyCallback::ErrorCode::ErrorCode as GetKeyErrorCode, |
| 19 | IGetKeyCallback::IGetKeyCallback, IGetRegistrationCallback::BnGetRegistrationCallback, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 20 | IGetRegistrationCallback::IGetRegistrationCallback, IRegistration::IRegistration, |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 21 | IRemoteProvisioning::IRemoteProvisioning, |
| 22 | IStoreUpgradedKeyCallback::BnStoreUpgradedKeyCallback, |
| 23 | IStoreUpgradedKeyCallback::IStoreUpgradedKeyCallback, |
| 24 | RemotelyProvisionedKey::RemotelyProvisionedKey, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 25 | }; |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 26 | use anyhow::{Context, Result}; |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 27 | use binder::{BinderFeatures, Interface, StatusCode, Strong}; |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 28 | use message_macro::source_location_msg; |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 29 | use std::sync::Mutex; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 30 | use std::time::Duration; |
| 31 | use tokio::sync::oneshot; |
| 32 | use tokio::time::timeout; |
| 33 | |
| 34 | // Normally, we block indefinitely when making calls outside of keystore and rely on watchdog to |
| 35 | // report deadlocks. However, RKPD is mainline updatable. Also, calls to RKPD may wait on network |
| 36 | // for certificates. So, we err on the side of caution and timeout instead. |
| 37 | static RKPD_TIMEOUT: Duration = Duration::from_secs(10); |
| 38 | |
| 39 | fn tokio_rt() -> tokio::runtime::Runtime { |
| 40 | tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap() |
| 41 | } |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 42 | |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 43 | /// Errors occurred during the interaction with RKPD. |
| 44 | #[derive(Debug, Clone, Copy, thiserror::Error, PartialEq, Eq)] |
| 45 | pub enum Error { |
| 46 | /// An RKPD request gets cancelled. |
| 47 | #[error("An RKPD request gets cancelled")] |
| 48 | RequestCancelled, |
| 49 | |
| 50 | /// Failed to get registration. |
| 51 | #[error("Failed to get registration")] |
| 52 | GetRegistrationFailed, |
| 53 | |
| 54 | /// Failed to get key. |
| 55 | #[error("Failed to get key: {0:?}")] |
| 56 | GetKeyFailed(GetKeyErrorCode), |
| 57 | |
| 58 | /// Failed to store upgraded key. |
| 59 | #[error("Failed to store upgraded key")] |
| 60 | StoreUpgradedKeyFailed, |
| 61 | |
| 62 | /// Retryable timeout when waiting for a callback. |
| 63 | #[error("Retryable timeout when waiting for a callback")] |
| 64 | RetryableTimeout, |
| 65 | |
| 66 | /// Timeout when waiting for a callback. |
| 67 | #[error("Timeout when waiting for a callback")] |
| 68 | Timeout, |
| 69 | |
| 70 | /// Wraps a Binder status code. |
| 71 | #[error("Binder transaction error {0:?}")] |
| 72 | BinderTransaction(StatusCode), |
| 73 | } |
| 74 | |
| 75 | impl From<StatusCode> for Error { |
| 76 | fn from(s: StatusCode) -> Self { |
| 77 | Self::BinderTransaction(s) |
| 78 | } |
| 79 | } |
| 80 | |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 81 | /// Thread-safe channel for sending a value once and only once. If a value has |
| 82 | /// already been send, subsequent calls to send will noop. |
| 83 | struct SafeSender<T> { |
| 84 | inner: Mutex<Option<oneshot::Sender<T>>>, |
| 85 | } |
| 86 | |
| 87 | impl<T> SafeSender<T> { |
| 88 | fn new(sender: oneshot::Sender<T>) -> Self { |
| 89 | Self { inner: Mutex::new(Some(sender)) } |
| 90 | } |
| 91 | |
| 92 | fn send(&self, value: T) { |
| 93 | if let Some(inner) = self.inner.lock().unwrap().take() { |
Tri Vo | 0e5fe2c | 2023-02-15 17:02:06 -0800 | [diff] [blame] | 94 | // It's possible for the corresponding receiver to time out and be dropped. In this |
| 95 | // case send() will fail. This error is not actionable though, so only log the error. |
| 96 | if inner.send(value).is_err() { |
| 97 | log::error!("SafeSender::send() failed"); |
| 98 | } |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 102 | |
| 103 | struct GetRegistrationCallback { |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 104 | registration_tx: SafeSender<Result<binder::Strong<dyn IRegistration>>>, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | impl GetRegistrationCallback { |
| 108 | pub fn new_native_binder( |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 109 | registration_tx: oneshot::Sender<Result<binder::Strong<dyn IRegistration>>>, |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 110 | ) -> Strong<dyn IGetRegistrationCallback> { |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 111 | let result: Self = |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 112 | GetRegistrationCallback { registration_tx: SafeSender::new(registration_tx) }; |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 113 | BnGetRegistrationCallback::new_binder(result, BinderFeatures::default()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | impl Interface for GetRegistrationCallback {} |
| 118 | |
| 119 | impl IGetRegistrationCallback for GetRegistrationCallback { |
| 120 | fn onSuccess(&self, registration: &Strong<dyn IRegistration>) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 121 | self.registration_tx.send(Ok(registration.clone())); |
| 122 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 123 | } |
| 124 | fn onCancel(&self) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 125 | log::warn!("IGetRegistrationCallback cancelled"); |
| 126 | self.registration_tx.send( |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 127 | Err(Error::RequestCancelled) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 128 | .context(source_location_msg!("GetRegistrationCallback cancelled.")), |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 129 | ); |
| 130 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 131 | } |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 132 | fn onError(&self, description: &str) -> binder::Result<()> { |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 133 | log::error!("IGetRegistrationCallback failed: '{description}'"); |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 134 | self.registration_tx.send( |
| 135 | Err(Error::GetRegistrationFailed) |
| 136 | .context(source_location_msg!("GetRegistrationCallback failed: {:?}", description)), |
| 137 | ); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 138 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /// Make a new connection to a IRegistration service. |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 143 | async fn get_rkpd_registration(rpc_name: &str) -> Result<binder::Strong<dyn IRegistration>> { |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 144 | let remote_provisioning: Strong<dyn IRemoteProvisioning> = |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 145 | binder::get_interface("remote_provisioning") |
| 146 | .map_err(Error::from) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 147 | .context(source_location_msg!("Trying to connect to IRemoteProvisioning service."))?; |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 148 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 149 | let (tx, rx) = oneshot::channel(); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 150 | let cb = GetRegistrationCallback::new_native_binder(tx); |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 151 | |
| 152 | remote_provisioning |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 153 | .getRegistration(rpc_name, &cb) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 154 | .context(source_location_msg!("Trying to get registration."))?; |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 155 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 156 | match timeout(RKPD_TIMEOUT, rx).await { |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 157 | Err(e) => Err(Error::Timeout).context(source_location_msg!("Waiting for RKPD: {:?}", e)), |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 158 | Ok(v) => v.unwrap(), |
| 159 | } |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 162 | struct GetKeyCallback { |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 163 | key_tx: SafeSender<Result<RemotelyProvisionedKey>>, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | impl GetKeyCallback { |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 167 | pub fn new_native_binder( |
| 168 | key_tx: oneshot::Sender<Result<RemotelyProvisionedKey>>, |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 169 | ) -> Strong<dyn IGetKeyCallback> { |
Seth Moore | a882c96 | 2023-01-09 16:55:10 -0800 | [diff] [blame] | 170 | let result: Self = GetKeyCallback { key_tx: SafeSender::new(key_tx) }; |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 171 | BnGetKeyCallback::new_binder(result, BinderFeatures::default()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | impl Interface for GetKeyCallback {} |
| 176 | |
| 177 | impl IGetKeyCallback for GetKeyCallback { |
| 178 | fn onSuccess(&self, key: &RemotelyProvisionedKey) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 179 | self.key_tx.send(Ok(RemotelyProvisionedKey { |
| 180 | keyBlob: key.keyBlob.clone(), |
| 181 | encodedCertChain: key.encodedCertChain.clone(), |
| 182 | })); |
| 183 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 184 | } |
| 185 | fn onCancel(&self) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 186 | log::warn!("IGetKeyCallback cancelled"); |
| 187 | self.key_tx.send( |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 188 | Err(Error::RequestCancelled).context(source_location_msg!("GetKeyCallback cancelled.")), |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 189 | ); |
| 190 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 191 | } |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 192 | fn onError(&self, error: GetKeyErrorCode, description: &str) -> binder::Result<()> { |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 193 | log::error!("IGetKeyCallback failed: {description}"); |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 194 | self.key_tx.send(Err(Error::GetKeyFailed(error)).context(source_location_msg!( |
Seth Moore | 484010a | 2023-01-31 11:22:26 -0800 | [diff] [blame] | 195 | "GetKeyCallback failed: {:?} {:?}", |
| 196 | error, |
| 197 | description |
| 198 | ))); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 199 | Ok(()) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 203 | async fn get_rkpd_attestation_key_from_registration_async( |
| 204 | registration: &Strong<dyn IRegistration>, |
| 205 | caller_uid: u32, |
| 206 | ) -> Result<RemotelyProvisionedKey> { |
| 207 | let (tx, rx) = oneshot::channel(); |
| 208 | let cb = GetKeyCallback::new_native_binder(tx); |
| 209 | |
| 210 | registration |
| 211 | .getKey(caller_uid.try_into().unwrap(), &cb) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 212 | .context(source_location_msg!("Trying to get key."))?; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 213 | |
| 214 | match timeout(RKPD_TIMEOUT, rx).await { |
Tri Vo | 0e5fe2c | 2023-02-15 17:02:06 -0800 | [diff] [blame] | 215 | Err(e) => { |
| 216 | // Make a best effort attempt to cancel the timed out request. |
| 217 | if let Err(e) = registration.cancelGetKey(&cb) { |
| 218 | log::error!("IRegistration::cancelGetKey failed: {:?}", e); |
| 219 | } |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 220 | Err(Error::RetryableTimeout) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 221 | .context(source_location_msg!("Waiting for RKPD key timed out: {:?}", e)) |
Tri Vo | 0e5fe2c | 2023-02-15 17:02:06 -0800 | [diff] [blame] | 222 | } |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 223 | Ok(v) => v.unwrap(), |
| 224 | } |
| 225 | } |
| 226 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 227 | async fn get_rkpd_attestation_key_async( |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 228 | rpc_name: &str, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 229 | caller_uid: u32, |
| 230 | ) -> Result<RemotelyProvisionedKey> { |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 231 | let registration = get_rkpd_registration(rpc_name) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 232 | .await |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 233 | .context(source_location_msg!("Trying to get to IRegistration service."))?; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 234 | get_rkpd_attestation_key_from_registration_async(®istration, caller_uid).await |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 237 | struct StoreUpgradedKeyCallback { |
| 238 | completer: SafeSender<Result<()>>, |
| 239 | } |
| 240 | |
| 241 | impl StoreUpgradedKeyCallback { |
| 242 | pub fn new_native_binder( |
| 243 | completer: oneshot::Sender<Result<()>>, |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 244 | ) -> Strong<dyn IStoreUpgradedKeyCallback> { |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 245 | let result: Self = StoreUpgradedKeyCallback { completer: SafeSender::new(completer) }; |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 246 | BnStoreUpgradedKeyCallback::new_binder(result, BinderFeatures::default()) |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | impl Interface for StoreUpgradedKeyCallback {} |
| 251 | |
| 252 | impl IStoreUpgradedKeyCallback for StoreUpgradedKeyCallback { |
| 253 | fn onSuccess(&self) -> binder::Result<()> { |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 254 | self.completer.send(Ok(())); |
| 255 | Ok(()) |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | fn onError(&self, error: &str) -> binder::Result<()> { |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 259 | log::error!("IStoreUpgradedKeyCallback failed: {error}"); |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 260 | self.completer.send( |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 261 | Err(Error::StoreUpgradedKeyFailed) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 262 | .context(source_location_msg!("Failed to store upgraded key: {:?}", error)), |
Seth Moore | 613a1fd | 2023-01-11 10:42:26 -0800 | [diff] [blame] | 263 | ); |
| 264 | Ok(()) |
Seth Moore | a55428e | 2023-01-10 13:07:31 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 268 | async fn store_rkpd_attestation_key_with_registration_async( |
| 269 | registration: &Strong<dyn IRegistration>, |
| 270 | key_blob: &[u8], |
| 271 | upgraded_blob: &[u8], |
| 272 | ) -> Result<()> { |
| 273 | let (tx, rx) = oneshot::channel(); |
| 274 | let cb = StoreUpgradedKeyCallback::new_native_binder(tx); |
| 275 | |
| 276 | registration |
| 277 | .storeUpgradedKeyAsync(key_blob, upgraded_blob, &cb) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 278 | .context(source_location_msg!("Failed to store upgraded blob with RKPD."))?; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 279 | |
| 280 | match timeout(RKPD_TIMEOUT, rx).await { |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 281 | Err(e) => Err(Error::Timeout) |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 282 | .context(source_location_msg!("Waiting for RKPD to complete storing key: {:?}", e)), |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 283 | Ok(v) => v.unwrap(), |
| 284 | } |
| 285 | } |
| 286 | |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 287 | async fn store_rkpd_attestation_key_async( |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 288 | rpc_name: &str, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 289 | key_blob: &[u8], |
| 290 | upgraded_blob: &[u8], |
| 291 | ) -> Result<()> { |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 292 | let registration = get_rkpd_registration(rpc_name) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 293 | .await |
Alice Wang | e66c331 | 2023-11-07 12:41:42 +0000 | [diff] [blame] | 294 | .context(source_location_msg!("Trying to get to IRegistration service."))?; |
Tri Vo | 437d014 | 2023-01-18 16:43:49 -0800 | [diff] [blame] | 295 | store_rkpd_attestation_key_with_registration_async(®istration, key_blob, upgraded_blob).await |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /// Get attestation key from RKPD. |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 299 | pub fn get_rkpd_attestation_key(rpc_name: &str, caller_uid: u32) -> Result<RemotelyProvisionedKey> { |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 300 | tokio_rt().block_on(get_rkpd_attestation_key_async(rpc_name, caller_uid)) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /// Store attestation key in RKPD. |
| 304 | pub fn store_rkpd_attestation_key( |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 305 | rpc_name: &str, |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 306 | key_blob: &[u8], |
| 307 | upgraded_blob: &[u8], |
| 308 | ) -> Result<()> { |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 309 | tokio_rt().block_on(store_rkpd_attestation_key_async(rpc_name, key_blob, upgraded_blob)) |
Tri Vo | e8f0444 | 2022-12-21 08:53:56 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | #[cfg(test)] |
David Drysdale | 2566fb3 | 2024-07-09 14:46:37 +0100 | [diff] [blame^] | 313 | mod tests; |