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