Alice Wang | 47287e7 | 2023-09-29 13:14:33 +0000 | [diff] [blame] | 1 | // 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 | |
Alice Wang | 8ba3c99 | 2023-11-15 15:07:48 +0000 | [diff] [blame] | 17 | use bssl_avf_error::{CipherError, EcError, EcdsaError, GlobalError, ReasonCode}; |
Maurice Lam | 0322b8c | 2023-12-18 22:13:48 +0000 | [diff] [blame^] | 18 | use bssl_sys::{self, ERR_get_error, ERR_GET_LIB_RUST, ERR_GET_REASON_RUST}; |
Alice Wang | 47287e7 | 2023-09-29 13:14:33 +0000 | [diff] [blame] | 19 | |
| 20 | const 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. |
| 24 | pub(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. |
| 35 | fn get_packed_error() -> u32 { |
| 36 | // SAFETY: This function only reads the error queue. |
| 37 | unsafe { ERR_get_error() } |
| 38 | } |
| 39 | |
| 40 | fn 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. |
| 46 | fn 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 | |
| 51 | fn 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. |
| 62 | fn map_global_reason_code(reason: i32) -> Option<GlobalError> { |
| 63 | let reason = match reason { |
Maurice Lam | 0322b8c | 2023-12-18 22:13:48 +0000 | [diff] [blame^] | 64 | bssl_sys::ERR_R_FATAL => GlobalError::Fatal, |
| 65 | bssl_sys::ERR_R_MALLOC_FAILURE => GlobalError::MallocFailure, |
| 66 | bssl_sys::ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED => GlobalError::ShouldNotHaveBeenCalled, |
| 67 | bssl_sys::ERR_R_PASSED_NULL_PARAMETER => GlobalError::PassedNullParameter, |
| 68 | bssl_sys::ERR_R_INTERNAL_ERROR => GlobalError::InternalError, |
| 69 | bssl_sys::ERR_R_OVERFLOW => GlobalError::Overflow, |
Alice Wang | 47287e7 | 2023-09-29 13:14:33 +0000 | [diff] [blame] | 70 | _ => return None, |
| 71 | }; |
| 72 | Some(reason) |
| 73 | } |
| 74 | |
| 75 | fn map_library_reason_code(reason: i32, lib: i32) -> Option<ReasonCode> { |
| 76 | u32::try_from(lib).ok().and_then(|x| match x { |
Maurice Lam | 0322b8c | 2023-12-18 22:13:48 +0000 | [diff] [blame^] | 77 | bssl_sys::ERR_LIB_CIPHER => map_cipher_reason_code(reason).map(ReasonCode::Cipher), |
| 78 | bssl_sys::ERR_LIB_EC => map_ec_reason_code(reason).map(ReasonCode::Ec), |
| 79 | bssl_sys::ERR_LIB_ECDSA => map_ecdsa_reason_code(reason).map(ReasonCode::Ecdsa), |
Alice Wang | 47287e7 | 2023-09-29 13:14:33 +0000 | [diff] [blame] | 80 | _ => None, |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | fn map_cipher_reason_code(reason: i32) -> Option<CipherError> { |
| 85 | let error = match reason { |
Maurice Lam | 0322b8c | 2023-12-18 22:13:48 +0000 | [diff] [blame^] | 86 | bssl_sys::CIPHER_R_AES_KEY_SETUP_FAILED => CipherError::AesKeySetupFailed, |
| 87 | bssl_sys::CIPHER_R_BAD_DECRYPT => CipherError::BadDecrypt, |
| 88 | bssl_sys::CIPHER_R_BAD_KEY_LENGTH => CipherError::BadKeyLength, |
| 89 | bssl_sys::CIPHER_R_BUFFER_TOO_SMALL => CipherError::BufferTooSmall, |
| 90 | bssl_sys::CIPHER_R_CTRL_NOT_IMPLEMENTED => CipherError::CtrlNotImplemented, |
| 91 | bssl_sys::CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED => { |
Alice Wang | 47287e7 | 2023-09-29 13:14:33 +0000 | [diff] [blame] | 92 | CipherError::CtrlOperationNotImplemented |
| 93 | } |
Maurice Lam | 0322b8c | 2023-12-18 22:13:48 +0000 | [diff] [blame^] | 94 | bssl_sys::CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH => { |
Alice Wang | 47287e7 | 2023-09-29 13:14:33 +0000 | [diff] [blame] | 95 | CipherError::DataNotMultipleOfBlockLength |
| 96 | } |
Maurice Lam | 0322b8c | 2023-12-18 22:13:48 +0000 | [diff] [blame^] | 97 | bssl_sys::CIPHER_R_INITIALIZATION_ERROR => CipherError::InitializationError, |
| 98 | bssl_sys::CIPHER_R_INPUT_NOT_INITIALIZED => CipherError::InputNotInitialized, |
| 99 | bssl_sys::CIPHER_R_INVALID_AD_SIZE => CipherError::InvalidAdSize, |
| 100 | bssl_sys::CIPHER_R_INVALID_KEY_LENGTH => CipherError::InvalidKeyLength, |
| 101 | bssl_sys::CIPHER_R_INVALID_NONCE_SIZE => CipherError::InvalidNonceSize, |
| 102 | bssl_sys::CIPHER_R_INVALID_OPERATION => CipherError::InvalidOperation, |
| 103 | bssl_sys::CIPHER_R_IV_TOO_LARGE => CipherError::IvTooLarge, |
| 104 | bssl_sys::CIPHER_R_NO_CIPHER_SET => CipherError::NoCipherSet, |
| 105 | bssl_sys::CIPHER_R_OUTPUT_ALIASES_INPUT => CipherError::OutputAliasesInput, |
| 106 | bssl_sys::CIPHER_R_TAG_TOO_LARGE => CipherError::TagTooLarge, |
| 107 | bssl_sys::CIPHER_R_TOO_LARGE => CipherError::TooLarge, |
| 108 | bssl_sys::CIPHER_R_WRONG_FINAL_BLOCK_LENGTH => CipherError::WrongFinalBlockLength, |
| 109 | bssl_sys::CIPHER_R_NO_DIRECTION_SET => CipherError::NoDirectionSet, |
| 110 | bssl_sys::CIPHER_R_INVALID_NONCE => CipherError::InvalidNonce, |
Alice Wang | 47287e7 | 2023-09-29 13:14:33 +0000 | [diff] [blame] | 111 | _ => return None, |
| 112 | }; |
| 113 | Some(error) |
| 114 | } |
Alice Wang | 8ba3c99 | 2023-11-15 15:07:48 +0000 | [diff] [blame] | 115 | |
| 116 | fn map_ec_reason_code(reason: i32) -> Option<EcError> { |
| 117 | let error = match reason { |
Maurice Lam | 0322b8c | 2023-12-18 22:13:48 +0000 | [diff] [blame^] | 118 | bssl_sys::EC_R_BUFFER_TOO_SMALL => EcError::BufferTooSmall, |
| 119 | bssl_sys::EC_R_COORDINATES_OUT_OF_RANGE => EcError::CoordinatesOutOfRange, |
| 120 | bssl_sys::EC_R_D2I_ECPKPARAMETERS_FAILURE => EcError::D2IEcpkparametersFailure, |
| 121 | bssl_sys::EC_R_EC_GROUP_NEW_BY_NAME_FAILURE => EcError::EcGroupNewByNameFailure, |
| 122 | bssl_sys::EC_R_GROUP2PKPARAMETERS_FAILURE => EcError::Group2PkparametersFailure, |
| 123 | bssl_sys::EC_R_I2D_ECPKPARAMETERS_FAILURE => EcError::I2DEcpkparametersFailure, |
| 124 | bssl_sys::EC_R_INCOMPATIBLE_OBJECTS => EcError::IncompatibleObjects, |
| 125 | bssl_sys::EC_R_INVALID_COMPRESSED_POINT => EcError::InvalidCompressedPoint, |
| 126 | bssl_sys::EC_R_INVALID_COMPRESSION_BIT => EcError::InvalidCompressionBit, |
| 127 | bssl_sys::EC_R_INVALID_ENCODING => EcError::InvalidEncoding, |
| 128 | bssl_sys::EC_R_INVALID_FIELD => EcError::InvalidField, |
| 129 | bssl_sys::EC_R_INVALID_FORM => EcError::InvalidForm, |
| 130 | bssl_sys::EC_R_INVALID_GROUP_ORDER => EcError::InvalidGroupOrder, |
| 131 | bssl_sys::EC_R_INVALID_PRIVATE_KEY => EcError::InvalidPrivateKey, |
| 132 | bssl_sys::EC_R_MISSING_PARAMETERS => EcError::MissingParameters, |
| 133 | bssl_sys::EC_R_MISSING_PRIVATE_KEY => EcError::MissingPrivateKey, |
| 134 | bssl_sys::EC_R_NON_NAMED_CURVE => EcError::NonNamedCurve, |
| 135 | bssl_sys::EC_R_NOT_INITIALIZED => EcError::NotInitialized, |
| 136 | bssl_sys::EC_R_PKPARAMETERS2GROUP_FAILURE => EcError::Pkparameters2GroupFailure, |
| 137 | bssl_sys::EC_R_POINT_AT_INFINITY => EcError::PointAtInfinity, |
| 138 | bssl_sys::EC_R_POINT_IS_NOT_ON_CURVE => EcError::PointIsNotOnCurve, |
| 139 | bssl_sys::EC_R_SLOT_FULL => EcError::SlotFull, |
| 140 | bssl_sys::EC_R_UNDEFINED_GENERATOR => EcError::UndefinedGenerator, |
| 141 | bssl_sys::EC_R_UNKNOWN_GROUP => EcError::UnknownGroup, |
| 142 | bssl_sys::EC_R_UNKNOWN_ORDER => EcError::UnknownOrder, |
| 143 | bssl_sys::EC_R_WRONG_ORDER => EcError::WrongOrder, |
| 144 | bssl_sys::EC_R_BIGNUM_OUT_OF_RANGE => EcError::BignumOutOfRange, |
| 145 | bssl_sys::EC_R_WRONG_CURVE_PARAMETERS => EcError::WrongCurveParameters, |
| 146 | bssl_sys::EC_R_DECODE_ERROR => EcError::DecodeError, |
| 147 | bssl_sys::EC_R_ENCODE_ERROR => EcError::EncodeError, |
| 148 | bssl_sys::EC_R_GROUP_MISMATCH => EcError::GroupMismatch, |
| 149 | bssl_sys::EC_R_INVALID_COFACTOR => EcError::InvalidCofactor, |
| 150 | bssl_sys::EC_R_PUBLIC_KEY_VALIDATION_FAILED => EcError::PublicKeyValidationFailed, |
| 151 | bssl_sys::EC_R_INVALID_SCALAR => EcError::InvalidScalar, |
Alice Wang | 8ba3c99 | 2023-11-15 15:07:48 +0000 | [diff] [blame] | 152 | _ => return None, |
| 153 | }; |
| 154 | Some(error) |
| 155 | } |
| 156 | |
| 157 | fn map_ecdsa_reason_code(reason: i32) -> Option<EcdsaError> { |
| 158 | let error = match reason { |
Maurice Lam | 0322b8c | 2023-12-18 22:13:48 +0000 | [diff] [blame^] | 159 | bssl_sys::ECDSA_R_BAD_SIGNATURE => EcdsaError::BadSignature, |
| 160 | bssl_sys::ECDSA_R_MISSING_PARAMETERS => EcdsaError::MissingParameters, |
| 161 | bssl_sys::ECDSA_R_NEED_NEW_SETUP_VALUES => EcdsaError::NeedNewSetupValues, |
| 162 | bssl_sys::ECDSA_R_NOT_IMPLEMENTED => EcdsaError::NotImplemented, |
| 163 | bssl_sys::ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED => { |
Alice Wang | 8ba3c99 | 2023-11-15 15:07:48 +0000 | [diff] [blame] | 164 | EcdsaError::RandomNumberGenerationFailed |
| 165 | } |
Maurice Lam | 0322b8c | 2023-12-18 22:13:48 +0000 | [diff] [blame^] | 166 | bssl_sys::ECDSA_R_ENCODE_ERROR => EcdsaError::EncodeError, |
| 167 | bssl_sys::ECDSA_R_TOO_MANY_ITERATIONS => EcdsaError::TooManyIterations, |
Alice Wang | 8ba3c99 | 2023-11-15 15:07:48 +0000 | [diff] [blame] | 168 | _ => return None, |
| 169 | }; |
| 170 | Some(error) |
| 171 | } |