• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 functions thrown in this library.
16 
17 use open_dice_cbor_bindgen::DiceResult;
18 use std::{fmt, result};
19 
20 #[cfg(feature = "std")]
21 use std::error::Error;
22 
23 /// Error type used by DICE.
24 #[derive(Debug)]
25 pub enum DiceError {
26     /// Provided input was invalid.
27     InvalidInput,
28     /// Provided buffer was too small.
29     BufferTooSmall(usize),
30     /// Platform error.
31     PlatformError,
32     /// Unsupported key algorithm.
33     UnsupportedKeyAlgorithm(coset::iana::Algorithm),
34     /// A failed fallible allocation. Used in no_std environments.
35     MemoryAllocationError,
36     /// DICE chain not found in artifacts.
37     DiceChainNotFound,
38 }
39 
40 /// This makes `DiceError` accepted by anyhow.
41 #[cfg(feature = "std")]
42 impl Error for DiceError {}
43 
44 impl fmt::Display for DiceError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result45     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46         match self {
47             Self::InvalidInput => write!(f, "Invalid input"),
48             Self::BufferTooSmall(buffer_required_size) => {
49                 write!(f, "Buffer too small; need {buffer_required_size} bytes")
50             }
51             Self::PlatformError => write!(f, "Platform error"),
52             Self::UnsupportedKeyAlgorithm(algorithm) => {
53                 write!(f, "Unsupported key algorithm: {algorithm:?}")
54             }
55             Self::MemoryAllocationError => write!(f, "Memory allocation failed"),
56             Self::DiceChainNotFound => write!(f, "DICE chain not found in artifacts"),
57         }
58     }
59 }
60 
61 /// DICE result type.
62 pub type Result<T> = result::Result<T, DiceError>;
63 
64 /// Checks the given `DiceResult`. Returns an error if it's not OK.
check_result(result: DiceResult, buffer_required_size: usize) -> Result<()>65 pub(crate) fn check_result(result: DiceResult, buffer_required_size: usize) -> Result<()> {
66     match result {
67         DiceResult::kDiceResultOk => Ok(()),
68         DiceResult::kDiceResultInvalidInput => Err(DiceError::InvalidInput),
69         DiceResult::kDiceResultBufferTooSmall => {
70             Err(DiceError::BufferTooSmall(buffer_required_size))
71         }
72         DiceResult::kDiceResultPlatformError => Err(DiceError::PlatformError),
73     }
74 }
75