blob: 1ee40c93c853561de16e661009cadb6f906ba675 [file] [log] [blame]
Alice Wang47287e72023-09-29 13:14:33 +00001// Copyright 2023, 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//! Wrappers of the error handling functions in BoringSSL err.h.
16
17use bssl_avf_error::{CipherError, GlobalError, ReasonCode};
18use bssl_ffi::{self, ERR_get_error, ERR_GET_LIB_RUST, ERR_GET_REASON_RUST};
19
20const NO_ERROR_REASON_CODE: i32 = 0;
21
22/// Returns the reason code for the least recent error and removes that
23/// error from the error queue.
24pub(crate) fn get_error_reason_code() -> ReasonCode {
25 let packed_error = get_packed_error();
26 let reason = get_reason(packed_error);
27 let lib = get_lib(packed_error);
28 map_to_reason_code(reason, lib)
29}
30
31/// Returns the packed error code for the least recent error and removes that
32/// error from the error queue.
33///
34/// Returns 0 if there are no errors in the queue.
35fn get_packed_error() -> u32 {
36 // SAFETY: This function only reads the error queue.
37 unsafe { ERR_get_error() }
38}
39
40fn get_reason(packed_error: u32) -> i32 {
41 // SAFETY: This function only reads the given error code.
42 unsafe { ERR_GET_REASON_RUST(packed_error) }
43}
44
45/// Returns the library code for the error.
46fn get_lib(packed_error: u32) -> i32 {
47 // SAFETY: This function only reads the given error code.
48 unsafe { ERR_GET_LIB_RUST(packed_error) }
49}
50
51fn map_to_reason_code(reason: i32, lib: i32) -> ReasonCode {
52 if reason == NO_ERROR_REASON_CODE {
53 return ReasonCode::NoError;
54 }
55 map_global_reason_code(reason)
56 .map(ReasonCode::Global)
57 .or_else(|| map_library_reason_code(reason, lib))
58 .unwrap_or(ReasonCode::Unknown(reason, lib))
59}
60
61/// Global errors may occur in any library.
62fn map_global_reason_code(reason: i32) -> Option<GlobalError> {
63 let reason = match reason {
64 bssl_ffi::ERR_R_FATAL => GlobalError::Fatal,
65 bssl_ffi::ERR_R_MALLOC_FAILURE => GlobalError::MallocFailure,
66 bssl_ffi::ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED => GlobalError::ShouldNotHaveBeenCalled,
67 bssl_ffi::ERR_R_PASSED_NULL_PARAMETER => GlobalError::PassedNullParameter,
68 bssl_ffi::ERR_R_INTERNAL_ERROR => GlobalError::InternalError,
69 bssl_ffi::ERR_R_OVERFLOW => GlobalError::Overflow,
70 _ => return None,
71 };
72 Some(reason)
73}
74
75fn map_library_reason_code(reason: i32, lib: i32) -> Option<ReasonCode> {
76 u32::try_from(lib).ok().and_then(|x| match x {
77 bssl_ffi::ERR_LIB_CIPHER => map_cipher_reason_code(reason).map(ReasonCode::Cipher),
78 _ => None,
79 })
80}
81
82fn map_cipher_reason_code(reason: i32) -> Option<CipherError> {
83 let error = match reason {
84 bssl_ffi::CIPHER_R_AES_KEY_SETUP_FAILED => CipherError::AesKeySetupFailed,
85 bssl_ffi::CIPHER_R_BAD_DECRYPT => CipherError::BadDecrypt,
86 bssl_ffi::CIPHER_R_BAD_KEY_LENGTH => CipherError::BadKeyLength,
87 bssl_ffi::CIPHER_R_BUFFER_TOO_SMALL => CipherError::BufferTooSmall,
88 bssl_ffi::CIPHER_R_CTRL_NOT_IMPLEMENTED => CipherError::CtrlNotImplemented,
89 bssl_ffi::CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED => {
90 CipherError::CtrlOperationNotImplemented
91 }
92 bssl_ffi::CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH => {
93 CipherError::DataNotMultipleOfBlockLength
94 }
95 bssl_ffi::CIPHER_R_INITIALIZATION_ERROR => CipherError::InitializationError,
96 bssl_ffi::CIPHER_R_INPUT_NOT_INITIALIZED => CipherError::InputNotInitialized,
97 bssl_ffi::CIPHER_R_INVALID_AD_SIZE => CipherError::InvalidAdSize,
98 bssl_ffi::CIPHER_R_INVALID_KEY_LENGTH => CipherError::InvalidKeyLength,
99 bssl_ffi::CIPHER_R_INVALID_NONCE_SIZE => CipherError::InvalidNonceSize,
100 bssl_ffi::CIPHER_R_INVALID_OPERATION => CipherError::InvalidOperation,
101 bssl_ffi::CIPHER_R_IV_TOO_LARGE => CipherError::IvTooLarge,
102 bssl_ffi::CIPHER_R_NO_CIPHER_SET => CipherError::NoCipherSet,
103 bssl_ffi::CIPHER_R_OUTPUT_ALIASES_INPUT => CipherError::OutputAliasesInput,
104 bssl_ffi::CIPHER_R_TAG_TOO_LARGE => CipherError::TagTooLarge,
105 bssl_ffi::CIPHER_R_TOO_LARGE => CipherError::TooLarge,
106 bssl_ffi::CIPHER_R_WRONG_FINAL_BLOCK_LENGTH => CipherError::WrongFinalBlockLength,
107 bssl_ffi::CIPHER_R_NO_DIRECTION_SET => CipherError::NoDirectionSet,
108 bssl_ffi::CIPHER_R_INVALID_NONCE => CipherError::InvalidNonce,
109 _ => return None,
110 };
111 Some(error)
112}