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 //! Errors and relating structs thrown by the BoringSSL wrapper library.
16 
17 #![cfg_attr(not(feature = "std"), no_std)]
18 
19 mod code;
20 
21 use core::{fmt, result};
22 use serde::{Deserialize, Serialize};
23 
24 pub use crate::code::{CipherError, EcError, EcdsaError, GlobalError, ReasonCode};
25 
26 /// libbssl_avf result type.
27 pub type Result<T> = result::Result<T, Error>;
28 
29 /// Error type used by libbssl_avf.
30 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
31 pub enum Error {
32     /// Failed to invoke a BoringSSL API.
33     CallFailed(ApiName, ReasonCode),
34 
35     /// An unexpected internal error occurred.
36     InternalError,
37 
38     /// Failed to decode the COSE_Key.
39     CoseKeyDecodingFailed,
40 
41     /// An error occurred when interacting with the coset crate.
42     CosetError,
43 
44     /// Unimplemented operation.
45     Unimplemented,
46 }
47 
48 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result49     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50         match self {
51             Self::CallFailed(api_name, reason) => {
52                 write!(f, "Failed to invoke the BoringSSL API: {api_name:?}. Reason: {reason}")
53             }
54             Self::InternalError => write!(f, "An unexpected internal error occurred"),
55             Self::CoseKeyDecodingFailed => write!(f, "Failed to decode the COSE_Key"),
56             Self::CosetError => {
57                 write!(f, "An error occurred when interacting with the coset crate")
58             }
59             Self::Unimplemented => write!(f, "Unimplemented operation"),
60         }
61     }
62 }
63 
64 impl From<coset::CoseError> for Error {
from(e: coset::CoseError) -> Self65     fn from(e: coset::CoseError) -> Self {
66         log::error!("Coset error: {e}");
67         Self::CosetError
68     }
69 }
70 
71 /// BoringSSL API names.
72 #[allow(missing_docs)]
73 #[allow(non_camel_case_types)]
74 #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
75 pub enum ApiName {
76     BN_new,
77     BN_bin2bn,
78     BN_bn2bin_padded,
79     CBB_flush,
80     CBB_len,
81     EC_GROUP_new_by_curve_name,
82     EC_KEY_check_key,
83     EC_KEY_generate_key,
84     EC_KEY_get0_group,
85     EC_KEY_get0_public_key,
86     EC_KEY_marshal_private_key,
87     EC_KEY_parse_private_key,
88     EC_KEY_new_by_curve_name,
89     EC_KEY_set_public_key_affine_coordinates,
90     EC_POINT_get_affine_coordinates,
91     ECDSA_SIG_from_bytes,
92     ECDSA_SIG_new,
93     ECDSA_SIG_set0,
94     ECDSA_sign,
95     ECDSA_size,
96     ECDSA_verify,
97     ED25519_verify,
98     EVP_AEAD_CTX_new,
99     EVP_AEAD_CTX_open,
100     EVP_AEAD_CTX_seal,
101     EVP_Digest,
102     EVP_MD_CTX_new,
103     EVP_PKEY_new,
104     EVP_PKEY_new_raw_public_key,
105     EVP_PKEY_set1_EC_KEY,
106     EVP_marshal_public_key,
107     EVP_DigestVerify,
108     EVP_DigestVerifyInit,
109     HKDF,
110     HMAC,
111     i2d_ECDSA_SIG,
112     RAND_bytes,
113     SHA256,
114 }
115