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