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 use core::fmt; 16 use serde::{Deserialize, Serialize}; 17 18 type BsslReasonCode = i32; 19 type BsslLibraryCode = i32; 20 21 /// BoringSSL reason code. 22 #[allow(missing_docs)] 23 #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] 24 pub enum ReasonCode { 25 NoError, 26 Global(GlobalError), 27 Cipher(CipherError), 28 Ec(EcError), 29 Ecdsa(EcdsaError), 30 Unknown(BsslReasonCode, BsslLibraryCode), 31 } 32 33 impl fmt::Display for ReasonCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 35 match self { 36 Self::NoError => write!(f, "No error in the BoringSSL error queue."), 37 Self::Unknown(code, lib) => { 38 write!(f, "Unknown reason code '{code}' from the library '{lib}'") 39 } 40 other => write!(f, "{other:?}"), 41 } 42 } 43 } 44 45 /// Global errors may occur in any library. 46 /// 47 /// The values are from: 48 /// boringssl/src/include/openssl/err.h 49 #[allow(missing_docs)] 50 #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] 51 pub enum GlobalError { 52 Fatal, 53 MallocFailure, 54 ShouldNotHaveBeenCalled, 55 PassedNullParameter, 56 InternalError, 57 Overflow, 58 } 59 60 impl fmt::Display for GlobalError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 62 write!(f, "A global error occurred: {self:?}") 63 } 64 } 65 66 /// Errors occurred in the Cipher functions. 67 /// 68 /// The values are from: 69 /// boringssl/src/include/openssl/cipher.h 70 #[allow(missing_docs)] 71 #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] 72 pub enum CipherError { 73 AesKeySetupFailed, 74 BadDecrypt, 75 BadKeyLength, 76 BufferTooSmall, 77 CtrlNotImplemented, 78 CtrlOperationNotImplemented, 79 DataNotMultipleOfBlockLength, 80 InitializationError, 81 InputNotInitialized, 82 InvalidAdSize, 83 InvalidKeyLength, 84 InvalidNonceSize, 85 InvalidOperation, 86 IvTooLarge, 87 NoCipherSet, 88 OutputAliasesInput, 89 TagTooLarge, 90 TooLarge, 91 WrongFinalBlockLength, 92 NoDirectionSet, 93 InvalidNonce, 94 } 95 96 impl From<CipherError> for ReasonCode { from(e: CipherError) -> ReasonCode97 fn from(e: CipherError) -> ReasonCode { 98 ReasonCode::Cipher(e) 99 } 100 } 101 102 impl fmt::Display for CipherError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result103 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 104 write!(f, "An error occurred in a Cipher function: {self:?}") 105 } 106 } 107 108 /// Errors occurred in the EC functions. 109 /// 110 /// The values are from: 111 /// boringssl/src/include/openssl/ec.h 112 #[allow(missing_docs)] 113 #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] 114 pub enum EcError { 115 BufferTooSmall, 116 CoordinatesOutOfRange, 117 D2IEcpkparametersFailure, 118 EcGroupNewByNameFailure, 119 Group2PkparametersFailure, 120 I2DEcpkparametersFailure, 121 IncompatibleObjects, 122 InvalidCompressedPoint, 123 InvalidCompressionBit, 124 InvalidEncoding, 125 InvalidField, 126 InvalidForm, 127 InvalidGroupOrder, 128 InvalidPrivateKey, 129 MissingParameters, 130 MissingPrivateKey, 131 NonNamedCurve, 132 NotInitialized, 133 Pkparameters2GroupFailure, 134 PointAtInfinity, 135 PointIsNotOnCurve, 136 SlotFull, 137 UndefinedGenerator, 138 UnknownGroup, 139 UnknownOrder, 140 WrongOrder, 141 BignumOutOfRange, 142 WrongCurveParameters, 143 DecodeError, 144 EncodeError, 145 GroupMismatch, 146 InvalidCofactor, 147 PublicKeyValidationFailed, 148 InvalidScalar, 149 } 150 151 impl From<EcError> for ReasonCode { from(e: EcError) -> ReasonCode152 fn from(e: EcError) -> ReasonCode { 153 ReasonCode::Ec(e) 154 } 155 } 156 157 impl fmt::Display for EcError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 159 write!(f, "An error occurred in an EC function: {self:?}") 160 } 161 } 162 163 /// Errors occurred in the ECDSA functions. 164 /// 165 /// The values are from: 166 /// boringssl/src/include/openssl/ecdsa.h 167 #[allow(missing_docs)] 168 #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] 169 pub enum EcdsaError { 170 BadSignature, 171 MissingParameters, 172 NeedNewSetupValues, 173 NotImplemented, 174 RandomNumberGenerationFailed, 175 EncodeError, 176 TooManyIterations, 177 } 178 179 impl From<EcdsaError> for ReasonCode { from(e: EcdsaError) -> ReasonCode180 fn from(e: EcdsaError) -> ReasonCode { 181 ReasonCode::Ecdsa(e) 182 } 183 } 184 185 impl fmt::Display for EcdsaError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result186 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 187 write!(f, "An error occurred in an ECDSA function: {self:?}") 188 } 189 } 190