Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 1 | // Copyright 2020, 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 | //! Keystore error provides convenience methods and types for Keystore error handling. |
| 16 | //! Clients of Keystore expect one of two error codes, i.e., a Keystore ResponseCode as |
| 17 | //! defined by the Keystore AIDL interface, or a Keymint ErrorCode as defined by |
| 18 | //! the Keymint HAL specification. |
| 19 | //! This crate provides `Error` which can wrap both. It is to be used |
| 20 | //! internally by Keystore to diagnose error conditions that need to be reported to |
| 21 | //! the client. To report the error condition to the client the Keystore AIDL |
| 22 | //! interface defines a wire type `Result` which is distinctly different from Rust's |
| 23 | //! `enum Result<T,E>`. |
| 24 | //! |
| 25 | //! This crate provides the convenience method `map_or_log_err` to convert `anyhow::Error` |
| 26 | //! into this wire type. In addition to handling the conversion of `Error` |
| 27 | //! to the `Result` wire type it handles any other error by mapping it to |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 28 | //! `ResponseCode::SYSTEM_ERROR` and logs any error condition. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 29 | //! |
| 30 | //! Keystore functions should use `anyhow::Result` to return error conditions, and |
| 31 | //! context should be added every time an error is forwarded. |
| 32 | |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 33 | pub use android_hardware_security_keymint::aidl::android::hardware::security::keymint::ErrorCode::ErrorCode; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 34 | pub use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 35 | use android_system_keystore2::binder::{ |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 36 | ExceptionCode, Result as BinderResult, Status as BinderStatus, StatusCode, |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 37 | }; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 38 | use keystore2_selinux as selinux; |
| 39 | use std::cmp::PartialEq; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 40 | |
| 41 | /// This is the main Keystore error type. It wraps the Keystore `ResponseCode` generated |
| 42 | /// from AIDL in the `Rc` variant and Keymint `ErrorCode` in the Km variant. |
| 43 | #[derive(Debug, thiserror::Error, PartialEq)] |
| 44 | pub enum Error { |
| 45 | /// Wraps a Keystore `ResponseCode` as defined by the Keystore AIDL interface specification. |
| 46 | #[error("Error::Rc({0:?})")] |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 47 | Rc(ResponseCode), |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 48 | /// Wraps a Keymint `ErrorCode` as defined by the Keymint AIDL interface specification. |
| 49 | #[error("Error::Km({0:?})")] |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 50 | Km(ErrorCode), |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 51 | /// Wraps a Binder exception code other than a service specific exception. |
| 52 | #[error("Binder exception code {0:?}, {1:?}")] |
| 53 | Binder(ExceptionCode, i32), |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 54 | /// Wraps a Binder status code. |
| 55 | #[error("Binder transaction error {0:?}")] |
| 56 | BinderTransaction(StatusCode), |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 57 | /// Wraps a Remote Provisioning ErrorCode as defined by the IRemotelyProvisionedComponent |
| 58 | /// AIDL interface spec. |
| 59 | #[error("Error::Rp({0:?})")] |
| 60 | Rp(ErrorCode), |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | impl Error { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 64 | /// Short hand for `Error::Rc(ResponseCode::SYSTEM_ERROR)` |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 65 | pub fn sys() -> Self { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 66 | Error::Rc(ResponseCode::SYSTEM_ERROR) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Seth Moore | 7ee79f9 | 2021-12-07 11:42:49 -0800 | [diff] [blame^] | 69 | /// Short hand for `Error::Rc(ResponseCode::PERMISSION_DENIED)` |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 70 | pub fn perm() -> Self { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 71 | Error::Rc(ResponseCode::PERMISSION_DENIED) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 72 | } |
Seth Moore | 7ee79f9 | 2021-12-07 11:42:49 -0800 | [diff] [blame^] | 73 | |
| 74 | /// Short hand for `Error::Rc(ResponseCode::OUT_OF_KEYS)` |
| 75 | pub fn out_of_keys() -> Self { |
| 76 | Error::Rc(ResponseCode::OUT_OF_KEYS) |
| 77 | } |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 80 | /// Helper function to map the binder status we get from calls into KeyMint |
| 81 | /// to a Keystore Error. We don't create an anyhow error here to make |
| 82 | /// it easier to evaluate KeyMint errors, which we must do in some cases, e.g., |
| 83 | /// when diagnosing authentication requirements, update requirements, and running |
| 84 | /// out of operation slots. |
| 85 | pub fn map_km_error<T>(r: BinderResult<T>) -> Result<T, Error> { |
| 86 | r.map_err(|s| { |
| 87 | match s.exception_code() { |
| 88 | ExceptionCode::SERVICE_SPECIFIC => { |
| 89 | let se = s.service_specific_error(); |
| 90 | if se < 0 { |
| 91 | // Negative service specific errors are KM error codes. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 92 | Error::Km(ErrorCode(s.service_specific_error())) |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 93 | } else { |
| 94 | // Non negative error codes cannot be KM error codes. |
| 95 | // So we create an `Error::Binder` variant to preserve |
| 96 | // the service specific error code for logging. |
| 97 | // `map_or_log_err` will map this on a system error, |
| 98 | // but not before logging the details to logcat. |
| 99 | Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se) |
| 100 | } |
| 101 | } |
| 102 | // We create `Error::Binder` to preserve the exception code |
| 103 | // for logging. |
| 104 | // `map_or_log_err` will map this on a system error. |
| 105 | e_code => Error::Binder(e_code, 0), |
| 106 | } |
| 107 | }) |
| 108 | } |
| 109 | |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 110 | /// Helper function to map the binder status we get from calls into a RemotelyProvisionedComponent |
| 111 | /// to a Keystore Error. We don't create an anyhow error here to make |
| 112 | /// it easier to evaluate service specific errors. |
| 113 | pub fn map_rem_prov_error<T>(r: BinderResult<T>) -> Result<T, Error> { |
| 114 | r.map_err(|s| match s.exception_code() { |
| 115 | ExceptionCode::SERVICE_SPECIFIC => Error::Rp(ErrorCode(s.service_specific_error())), |
| 116 | e_code => Error::Binder(e_code, 0), |
| 117 | }) |
| 118 | } |
| 119 | |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 120 | /// This function is similar to map_km_error only that we don't expect |
| 121 | /// any KeyMint error codes, we simply preserve the exception code and optional |
| 122 | /// service specific exception. |
| 123 | pub fn map_binder_status<T>(r: BinderResult<T>) -> Result<T, Error> { |
| 124 | r.map_err(|s| match s.exception_code() { |
| 125 | ExceptionCode::SERVICE_SPECIFIC => { |
| 126 | let se = s.service_specific_error(); |
| 127 | Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se) |
| 128 | } |
| 129 | ExceptionCode::TRANSACTION_FAILED => { |
| 130 | let e = s.transaction_error(); |
| 131 | Error::BinderTransaction(e) |
| 132 | } |
| 133 | e_code => Error::Binder(e_code, 0), |
| 134 | }) |
| 135 | } |
| 136 | |
| 137 | /// This function maps a status code onto a Keystore Error. |
| 138 | pub fn map_binder_status_code<T>(r: Result<T, StatusCode>) -> Result<T, Error> { |
| 139 | r.map_err(Error::BinderTransaction) |
| 140 | } |
| 141 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 142 | /// This function should be used by Keystore service calls to translate error conditions |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 143 | /// into service specific exceptions. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 144 | /// |
Hasini Gunasinghe | e1d1bbd | 2021-04-20 18:13:25 +0000 | [diff] [blame] | 145 | /// All error conditions get logged by this function, except for KEY_NOT_FOUND error. |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 146 | /// |
| 147 | /// All `Error::Rc(x)` and `Error::Km(x)` variants get mapped onto a service specific error |
| 148 | /// code of x. This is possible because KeyMint `ErrorCode` errors are always negative and |
| 149 | /// `ResponseCode` codes are always positive. |
| 150 | /// `selinux::Error::PermissionDenied` is mapped on `ResponseCode::PERMISSION_DENIED`. |
| 151 | /// |
| 152 | /// All non `Error` error conditions and the Error::Binder variant get mapped onto |
| 153 | /// ResponseCode::SYSTEM_ERROR`. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 154 | /// |
| 155 | /// `handle_ok` will be called if `result` is `Ok(value)` where `value` will be passed |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 156 | /// as argument to `handle_ok`. `handle_ok` must generate a `BinderResult<T>`, but it |
| 157 | /// typically returns Ok(value). |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 158 | /// |
| 159 | /// # Examples |
| 160 | /// |
| 161 | /// ``` |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 162 | /// fn loadKey() -> anyhow::Result<Vec<u8>> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 163 | /// if (good_but_auth_required) { |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 164 | /// Ok(vec!['k', 'e', 'y']) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 165 | /// } else { |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 166 | /// Err(anyhow!(Error::Rc(ResponseCode::KEY_NOT_FOUND))) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 167 | /// } |
| 168 | /// } |
| 169 | /// |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 170 | /// map_or_log_err(loadKey(), Ok) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 171 | /// ``` |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 172 | pub fn map_or_log_err<T, U, F>(result: anyhow::Result<U>, handle_ok: F) -> BinderResult<T> |
| 173 | where |
| 174 | F: FnOnce(U) -> BinderResult<T>, |
| 175 | { |
Janis Danisevskis | 778245c | 2021-03-04 15:40:23 -0800 | [diff] [blame] | 176 | map_err_with( |
| 177 | result, |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 178 | |e| { |
Hasini Gunasinghe | e1d1bbd | 2021-04-20 18:13:25 +0000 | [diff] [blame] | 179 | // Make the key not found errors silent. |
| 180 | if !matches!( |
| 181 | e.root_cause().downcast_ref::<Error>(), |
| 182 | Some(Error::Rc(ResponseCode::KEY_NOT_FOUND)) |
| 183 | ) { |
| 184 | log::error!("{:?}", e); |
| 185 | } |
Janis Danisevskis | 778245c | 2021-03-04 15:40:23 -0800 | [diff] [blame] | 186 | e |
| 187 | }, |
| 188 | handle_ok, |
| 189 | ) |
| 190 | } |
| 191 | |
| 192 | /// This function behaves similar to map_or_log_error, but it does not log the errors, instead |
| 193 | /// it calls map_err on the error before mapping it to a binder result allowing callers to |
| 194 | /// log or transform the error before mapping it. |
| 195 | pub fn map_err_with<T, U, F1, F2>( |
| 196 | result: anyhow::Result<U>, |
| 197 | map_err: F1, |
| 198 | handle_ok: F2, |
| 199 | ) -> BinderResult<T> |
| 200 | where |
| 201 | F1: FnOnce(anyhow::Error) -> anyhow::Error, |
| 202 | F2: FnOnce(U) -> BinderResult<T>, |
| 203 | { |
| 204 | result.map_or_else( |
| 205 | |e| { |
| 206 | let e = map_err(e); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 207 | let rc = get_error_code(&e); |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 208 | Err(BinderStatus::new_service_specific_error(rc, None)) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 209 | }, |
| 210 | handle_ok, |
| 211 | ) |
| 212 | } |
| 213 | |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 214 | /// Returns the error code given a reference to the error |
| 215 | pub fn get_error_code(e: &anyhow::Error) -> i32 { |
| 216 | let root_cause = e.root_cause(); |
| 217 | match root_cause.downcast_ref::<Error>() { |
| 218 | Some(Error::Rc(rcode)) => rcode.0, |
| 219 | Some(Error::Km(ec)) => ec.0, |
| 220 | Some(Error::Rp(_)) => ResponseCode::SYSTEM_ERROR.0, |
| 221 | // If an Error::Binder reaches this stage we report a system error. |
| 222 | // The exception code and possible service specific error will be |
| 223 | // printed in the error log above. |
| 224 | Some(Error::Binder(_, _)) | Some(Error::BinderTransaction(_)) => { |
| 225 | ResponseCode::SYSTEM_ERROR.0 |
| 226 | } |
| 227 | None => match root_cause.downcast_ref::<selinux::Error>() { |
| 228 | Some(selinux::Error::PermissionDenied) => ResponseCode::PERMISSION_DENIED.0, |
| 229 | _ => ResponseCode::SYSTEM_ERROR.0, |
| 230 | }, |
| 231 | } |
| 232 | } |
| 233 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 234 | #[cfg(test)] |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 235 | pub mod tests { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 236 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 237 | use super::*; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 238 | use android_system_keystore2::binder::{ |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 239 | ExceptionCode, Result as BinderResult, Status as BinderStatus, |
| 240 | }; |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 241 | use anyhow::{anyhow, Context}; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 242 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 243 | fn nested_nested_rc(rc: ResponseCode) -> anyhow::Result<()> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 244 | Err(anyhow!(Error::Rc(rc))).context("nested nested rc") |
| 245 | } |
| 246 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 247 | fn nested_rc(rc: ResponseCode) -> anyhow::Result<()> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 248 | nested_nested_rc(rc).context("nested rc") |
| 249 | } |
| 250 | |
| 251 | fn nested_nested_ec(ec: ErrorCode) -> anyhow::Result<()> { |
| 252 | Err(anyhow!(Error::Km(ec))).context("nested nested ec") |
| 253 | } |
| 254 | |
| 255 | fn nested_ec(ec: ErrorCode) -> anyhow::Result<()> { |
| 256 | nested_nested_ec(ec).context("nested ec") |
| 257 | } |
| 258 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 259 | fn nested_nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 260 | Ok(rc) |
| 261 | } |
| 262 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 263 | fn nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 264 | nested_nested_ok(rc).context("nested ok") |
| 265 | } |
| 266 | |
Janis Danisevskis | ce99543 | 2020-07-21 12:22:34 -0700 | [diff] [blame] | 267 | fn nested_nested_selinux_perm() -> anyhow::Result<()> { |
| 268 | Err(anyhow!(selinux::Error::perm())).context("nested nexted selinux permission denied") |
| 269 | } |
| 270 | |
| 271 | fn nested_selinux_perm() -> anyhow::Result<()> { |
| 272 | nested_nested_selinux_perm().context("nested selinux permission denied") |
| 273 | } |
| 274 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 275 | #[derive(Debug, thiserror::Error)] |
| 276 | enum TestError { |
| 277 | #[error("TestError::Fail")] |
| 278 | Fail = 0, |
| 279 | } |
| 280 | |
| 281 | fn nested_nested_other_error() -> anyhow::Result<()> { |
| 282 | Err(anyhow!(TestError::Fail)).context("nested nested other error") |
| 283 | } |
| 284 | |
| 285 | fn nested_other_error() -> anyhow::Result<()> { |
| 286 | nested_nested_other_error().context("nested other error") |
| 287 | } |
| 288 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 289 | fn binder_sse_error(sse: i32) -> BinderResult<()> { |
| 290 | Err(BinderStatus::new_service_specific_error(sse, None)) |
| 291 | } |
| 292 | |
| 293 | fn binder_exception(ex: ExceptionCode) -> BinderResult<()> { |
| 294 | Err(BinderStatus::new_exception(ex, None)) |
| 295 | } |
| 296 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 297 | #[test] |
| 298 | fn keystore_error_test() -> anyhow::Result<(), String> { |
| 299 | android_logger::init_once( |
| 300 | android_logger::Config::default() |
| 301 | .with_tag("keystore_error_tests") |
| 302 | .with_min_level(log::Level::Debug), |
| 303 | ); |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 304 | // All Error::Rc(x) get mapped on a service specific error |
| 305 | // code of x. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 306 | for rc in ResponseCode::LOCKED.0..ResponseCode::BACKEND_BUSY.0 { |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 307 | assert_eq!( |
| 308 | Result::<(), i32>::Err(rc), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 309 | map_or_log_err(nested_rc(ResponseCode(rc)), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 310 | .map_err(|s| s.service_specific_error()) |
| 311 | ); |
| 312 | } |
| 313 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 314 | // All Keystore Error::Km(x) get mapped on a service |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 315 | // specific error of x. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 316 | for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 { |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 317 | assert_eq!( |
| 318 | Result::<(), i32>::Err(ec), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 319 | map_or_log_err(nested_ec(ErrorCode(ec)), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 320 | .map_err(|s| s.service_specific_error()) |
| 321 | ); |
| 322 | } |
| 323 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 324 | // All Keymint errors x received through a Binder Result get mapped on |
| 325 | // a service specific error of x. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 326 | for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 { |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 327 | assert_eq!( |
| 328 | Result::<(), i32>::Err(ec), |
| 329 | map_or_log_err( |
| 330 | map_km_error(binder_sse_error(ec)) |
| 331 | .with_context(|| format!("Km error code: {}.", ec)), |
| 332 | |_| Err(BinderStatus::ok()) |
| 333 | ) |
| 334 | .map_err(|s| s.service_specific_error()) |
| 335 | ); |
| 336 | } |
| 337 | |
| 338 | // map_km_error creates an Error::Binder variant storing |
| 339 | // ExceptionCode::SERVICE_SPECIFIC and the given |
| 340 | // service specific error. |
| 341 | let sse = map_km_error(binder_sse_error(1)); |
| 342 | assert_eq!(Err(Error::Binder(ExceptionCode::SERVICE_SPECIFIC, 1)), sse); |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 343 | // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR. |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 344 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 345 | Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR), |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 346 | map_or_log_err(sse.context("Non negative service specific error."), |_| Err( |
| 347 | BinderStatus::ok() |
| 348 | )) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 349 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 350 | ); |
| 351 | |
| 352 | // map_km_error creates a Error::Binder variant storing the given exception code. |
| 353 | let binder_exception = map_km_error(binder_exception(ExceptionCode::TRANSACTION_FAILED)); |
| 354 | assert_eq!(Err(Error::Binder(ExceptionCode::TRANSACTION_FAILED, 0)), binder_exception); |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 355 | // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR. |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 356 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 357 | Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR), |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 358 | map_or_log_err(binder_exception.context("Binder Exception."), |_| Err( |
| 359 | BinderStatus::ok() |
| 360 | )) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 361 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 362 | ); |
| 363 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 364 | // selinux::Error::Perm() needs to be mapped to ResponseCode::PERMISSION_DENIED |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 365 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 366 | Result::<(), ResponseCode>::Err(ResponseCode::PERMISSION_DENIED), |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 367 | map_or_log_err(nested_selinux_perm(), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 368 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 369 | ); |
| 370 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 371 | // All other errors get mapped on System Error. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 372 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 373 | Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR), |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 374 | map_or_log_err(nested_other_error(), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 375 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 376 | ); |
| 377 | |
| 378 | // Result::Ok variants get passed to the ok handler. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 379 | assert_eq!(Ok(ResponseCode::LOCKED), map_or_log_err(nested_ok(ResponseCode::LOCKED), Ok)); |
| 380 | assert_eq!( |
| 381 | Ok(ResponseCode::SYSTEM_ERROR), |
| 382 | map_or_log_err(nested_ok(ResponseCode::SYSTEM_ERROR), Ok) |
| 383 | ); |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 384 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 385 | Ok(()) |
| 386 | } |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 387 | |
| 388 | //Helper function to test whether error cases are handled as expected. |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 389 | pub fn check_result_contains_error_string<T>( |
| 390 | result: anyhow::Result<T>, |
| 391 | expected_error_string: &str, |
| 392 | ) { |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 393 | let error_str = format!( |
| 394 | "{:#?}", |
| 395 | result.err().unwrap_or_else(|| panic!("Expected the error: {}", expected_error_string)) |
| 396 | ); |
| 397 | assert!( |
| 398 | error_str.contains(expected_error_string), |
| 399 | "The string \"{}\" should contain \"{}\"", |
| 400 | error_str, |
| 401 | expected_error_string |
| 402 | ); |
| 403 | } |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 404 | } // mod tests |