blob: d57ba0c1cbd6758c786b4db45a76cbd8690e376e [file] [log] [blame]
Janis Danisevskis7d77a762020-07-20 13:03:31 -07001// 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 Danisevskis7d77a762020-07-20 13:03:31 -070016//!
Tri Vocd6fc7a2023-08-31 11:46:32 -040017//! Here are some important types and helper functions:
Janis Danisevskis7d77a762020-07-20 13:03:31 -070018//!
Tri Vocd6fc7a2023-08-31 11:46:32 -040019//! `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//!
David Drysdaledb7ddde2024-06-07 16:22:49 +010024//! `into_[logged_]binder` is a convenience method used to convert `anyhow::Error` into
25//! `SerializedError` wire type.
Tri Vocd6fc7a2023-08-31 11:46:32 -040026//!
27//! Keystore functions should use `anyhow::Result` to return error conditions, and context should
28//! be added every time an error is forwarded.
Janis Danisevskis7d77a762020-07-20 13:03:31 -070029
Shawn Willden708744a2020-12-11 13:05:27 +000030pub use android_hardware_security_keymint::aidl::android::hardware::security::keymint::ErrorCode::ErrorCode;
Alice Wang849cfe42023-11-10 12:43:36 +000031use android_security_rkp_aidl::aidl::android::security::rkp::IGetKeyCallback::ErrorCode::ErrorCode as GetKeyErrorCode;
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070032pub use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode;
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070033use android_system_keystore2::binder::{
Janis Danisevskisba998992020-12-29 16:08:40 -080034 ExceptionCode, Result as BinderResult, Status as BinderStatus, StatusCode,
Janis Danisevskis017d2092020-09-02 10:15:52 -070035};
Janis Danisevskis2ee014b2021-05-05 14:29:08 -070036use keystore2_selinux as selinux;
Vikram Gaur743f1782024-09-06 05:45:08 +000037use postprocessor_client::Error as PostProcessorError;
Alice Wang01c16b62023-11-07 14:27:49 +000038use rkpd_client::Error as RkpdError;
Janis Danisevskis2ee014b2021-05-05 14:29:08 -070039use std::cmp::PartialEq;
Janis Danisevskisea03cff2021-12-16 08:10:17 -080040use std::ffi::CString;
Janis Danisevskis7d77a762020-07-20 13:03:31 -070041
David Drysdale2566fb32024-07-09 14:46:37 +010042#[cfg(test)]
43pub mod tests;
44
Janis Danisevskis7d77a762020-07-20 13:03:31 -070045/// This is the main Keystore error type. It wraps the Keystore `ResponseCode` generated
46/// from AIDL in the `Rc` variant and Keymint `ErrorCode` in the Km variant.
Chris Wailes263de9f2022-08-11 15:00:51 -070047#[derive(Debug, thiserror::Error, PartialEq, Eq)]
Janis Danisevskis7d77a762020-07-20 13:03:31 -070048pub enum Error {
49 /// Wraps a Keystore `ResponseCode` as defined by the Keystore AIDL interface specification.
50 #[error("Error::Rc({0:?})")]
Janis Danisevskise24f3472020-08-12 17:58:49 -070051 Rc(ResponseCode),
Janis Danisevskis7d77a762020-07-20 13:03:31 -070052 /// Wraps a Keymint `ErrorCode` as defined by the Keymint AIDL interface specification.
53 #[error("Error::Km({0:?})")]
Janis Danisevskise24f3472020-08-12 17:58:49 -070054 Km(ErrorCode),
Janis Danisevskis017d2092020-09-02 10:15:52 -070055 /// Wraps a Binder exception code other than a service specific exception.
56 #[error("Binder exception code {0:?}, {1:?}")]
57 Binder(ExceptionCode, i32),
Janis Danisevskisba998992020-12-29 16:08:40 -080058 /// Wraps a Binder status code.
59 #[error("Binder transaction error {0:?}")]
60 BinderTransaction(StatusCode),
Janis Danisevskis7d77a762020-07-20 13:03:31 -070061}
62
63impl Error {
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070064 /// Short hand for `Error::Rc(ResponseCode::SYSTEM_ERROR)`
Janis Danisevskis7d77a762020-07-20 13:03:31 -070065 pub fn sys() -> Self {
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070066 Error::Rc(ResponseCode::SYSTEM_ERROR)
Janis Danisevskis7d77a762020-07-20 13:03:31 -070067 }
68
Seth Moore7ee79f92021-12-07 11:42:49 -080069 /// Short hand for `Error::Rc(ResponseCode::PERMISSION_DENIED)`
Janis Danisevskis7d77a762020-07-20 13:03:31 -070070 pub fn perm() -> Self {
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070071 Error::Rc(ResponseCode::PERMISSION_DENIED)
Janis Danisevskis7d77a762020-07-20 13:03:31 -070072 }
73}
74
Alice Wang849cfe42023-11-10 12:43:36 +000075impl From<RkpdError> for Error {
76 fn from(e: RkpdError) -> Self {
77 match e {
78 RkpdError::RequestCancelled | RkpdError::GetRegistrationFailed => {
79 Error::Rc(ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR)
80 }
81 RkpdError::GetKeyFailed(e) => {
82 let response_code = match e {
83 GetKeyErrorCode::ERROR_UNKNOWN => ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR,
84 GetKeyErrorCode::ERROR_PERMANENT => ResponseCode::OUT_OF_KEYS_PERMANENT_ERROR,
85 GetKeyErrorCode::ERROR_PENDING_INTERNET_CONNECTIVITY => {
86 ResponseCode::OUT_OF_KEYS_PENDING_INTERNET_CONNECTIVITY
87 }
88 GetKeyErrorCode::ERROR_REQUIRES_SECURITY_PATCH => {
89 ResponseCode::OUT_OF_KEYS_REQUIRES_SYSTEM_UPGRADE
90 }
91 _ => {
92 log::error!("Unexpected get key error from rkpd: {e:?}");
93 ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR
94 }
95 };
96 Error::Rc(response_code)
97 }
98 RkpdError::RetryableTimeout => Error::Rc(ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR),
99 RkpdError::StoreUpgradedKeyFailed | RkpdError::Timeout => {
100 Error::Rc(ResponseCode::SYSTEM_ERROR)
101 }
102 RkpdError::BinderTransaction(s) => Error::BinderTransaction(s),
103 }
104 }
105}
106
Vikram Gaur743f1782024-09-06 05:45:08 +0000107impl From<PostProcessorError> for Error {
108 fn from(e: PostProcessorError) -> Self {
109 match e {
110 PostProcessorError(s) => Error::BinderTransaction(s),
111 }
112 }
113}
114
Alice Wang849cfe42023-11-10 12:43:36 +0000115/// Maps an `rkpd_client::Error` that is wrapped with an `anyhow::Error` to a keystore2 `Error`.
116pub fn wrapped_rkpd_error_to_ks_error(e: &anyhow::Error) -> Error {
117 match e.downcast_ref::<RkpdError>() {
118 Some(e) => Error::from(*e),
119 None => {
120 log::error!("Failed to downcast the anyhow::Error to rkpd_client::Error: {e:?}");
121 Error::Rc(ResponseCode::SYSTEM_ERROR)
122 }
123 }
124}
125
Janis Danisevskis017d2092020-09-02 10:15:52 -0700126/// Helper function to map the binder status we get from calls into KeyMint
127/// to a Keystore Error. We don't create an anyhow error here to make
128/// it easier to evaluate KeyMint errors, which we must do in some cases, e.g.,
129/// when diagnosing authentication requirements, update requirements, and running
130/// out of operation slots.
131pub fn map_km_error<T>(r: BinderResult<T>) -> Result<T, Error> {
132 r.map_err(|s| {
133 match s.exception_code() {
134 ExceptionCode::SERVICE_SPECIFIC => {
135 let se = s.service_specific_error();
136 if se < 0 {
137 // Negative service specific errors are KM error codes.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700138 Error::Km(ErrorCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -0700139 } else {
140 // Non negative error codes cannot be KM error codes.
141 // So we create an `Error::Binder` variant to preserve
142 // the service specific error code for logging.
Janis Danisevskis017d2092020-09-02 10:15:52 -0700143 Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se)
144 }
145 }
146 // We create `Error::Binder` to preserve the exception code
147 // for logging.
Janis Danisevskis017d2092020-09-02 10:15:52 -0700148 e_code => Error::Binder(e_code, 0),
149 }
150 })
151}
152
Janis Danisevskisba998992020-12-29 16:08:40 -0800153/// This function is similar to map_km_error only that we don't expect
154/// any KeyMint error codes, we simply preserve the exception code and optional
155/// service specific exception.
156pub fn map_binder_status<T>(r: BinderResult<T>) -> Result<T, Error> {
157 r.map_err(|s| match s.exception_code() {
158 ExceptionCode::SERVICE_SPECIFIC => {
159 let se = s.service_specific_error();
160 Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se)
161 }
162 ExceptionCode::TRANSACTION_FAILED => {
163 let e = s.transaction_error();
164 Error::BinderTransaction(e)
165 }
166 e_code => Error::Binder(e_code, 0),
167 })
168}
169
170/// This function maps a status code onto a Keystore Error.
171pub fn map_binder_status_code<T>(r: Result<T, StatusCode>) -> Result<T, Error> {
172 r.map_err(Error::BinderTransaction)
173}
174
David Drysdaledb7ddde2024-06-07 16:22:49 +0100175/// Convert an [`anyhow::Error`] to a [`binder::Status`], logging the value
176/// along the way (except if it is `KEY_NOT_FOUND`).
177pub fn into_logged_binder(e: anyhow::Error) -> BinderStatus {
178 // Log everything except key not found.
179 if !matches!(
180 e.root_cause().downcast_ref::<Error>(),
181 Some(Error::Rc(ResponseCode::KEY_NOT_FOUND))
182 ) {
183 log::error!("{:?}", e);
184 }
185 into_binder(e)
Janis Danisevskis778245c2021-03-04 15:40:23 -0800186}
187
Janis Danisevskisea03cff2021-12-16 08:10:17 -0800188/// This function turns an anyhow error into an optional CString.
189/// This is especially useful to add a message string to a service specific error.
190/// If the formatted string was not convertible because it contained a nul byte,
191/// None is returned and a warning is logged.
192pub fn anyhow_error_to_cstring(e: &anyhow::Error) -> Option<CString> {
193 match CString::new(format!("{:?}", e)) {
194 Ok(msg) => Some(msg),
195 Err(_) => {
196 log::warn!("Cannot convert error message to CStr. It contained a nul byte.");
197 None
198 }
199 }
200}
201
David Drysdaledb7ddde2024-06-07 16:22:49 +0100202/// Convert an [`anyhow::Error`] to a [`binder::Status`].
203pub fn into_binder(e: anyhow::Error) -> binder::Status {
204 let rc = anyhow_error_to_serialized_error(&e);
205 BinderStatus::new_service_specific_error(rc.0, anyhow_error_to_cstring(&e).as_deref())
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700206}
207
Tri Vocd6fc7a2023-08-31 11:46:32 -0400208/// This type is used to send error codes on the wire.
209///
210/// Errors are squashed into one number space using following rules:
211/// - All Keystore and Keymint errors codes are identity mapped. It's possible because by
212/// convention Keystore `ResponseCode` errors are positive, and Keymint `ErrorCode` errors are
213/// negative.
214/// - `selinux::Error::PermissionDenied` is mapped to `ResponseCode::PERMISSION_DENIED`.
215/// - All other error conditions, e.g. Binder errors, are mapped to `ResponseCode::SYSTEM_ERROR`.
216///
217/// The type should be used to forward all error codes to clients of Keystore AIDL interface and to
218/// metrics events.
219#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
220pub struct SerializedError(pub i32);
221
222/// Returns a SerializedError given a reference to Error.
223pub fn error_to_serialized_error(e: &Error) -> SerializedError {
224 match e {
225 Error::Rc(rcode) => SerializedError(rcode.0),
226 Error::Km(ec) => SerializedError(ec.0),
227 // Binder errors are reported as system error.
228 Error::Binder(_, _) | Error::BinderTransaction(_) => {
229 SerializedError(ResponseCode::SYSTEM_ERROR.0)
230 }
231 }
232}
233
234/// Returns a SerializedError given a reference to anyhow::Error.
235pub fn anyhow_error_to_serialized_error(e: &anyhow::Error) -> SerializedError {
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000236 let root_cause = e.root_cause();
237 match root_cause.downcast_ref::<Error>() {
Tri Vocd6fc7a2023-08-31 11:46:32 -0400238 Some(e) => error_to_serialized_error(e),
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000239 None => match root_cause.downcast_ref::<selinux::Error>() {
Tri Vocd6fc7a2023-08-31 11:46:32 -0400240 Some(selinux::Error::PermissionDenied) => {
241 SerializedError(ResponseCode::PERMISSION_DENIED.0)
242 }
243 _ => SerializedError(ResponseCode::SYSTEM_ERROR.0),
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000244 },
245 }
246}