xref: /aosp_15_r20/external/open-dice/dpe-rs/src/error.rs (revision 60b67249c2e226f42f35cc6cfe66c6048e0bae6b)
1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 //! Defines the [ErrCode] and [DpeResult] types.
16 
17 use log::error;
18 
19 /// An enum of error codes as defined in the DPE specification. The
20 /// discriminant values match the CBOR encoding values per the specification.
21 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
22 pub enum ErrCode {
23     /// An unexpected error has occurred which is not actionable by the client.
24     InternalError = 1,
25     /// The command could not be decrypted, parsed, or is not supported.
26     InvalidCommand = 2,
27     /// A command argument is malformed, invalid with respect to the current
28     /// DPE state, in conflict with other arguments, not allowed, not
29     /// recognized, or otherwise not supported.
30     InvalidArgument = 3,
31     /// Keys for an encrypted session have been exhausted.
32     SessionExhausted = 4,
33     /// The command cannot be fulfilled because an internal seed component is
34     /// no longer available.
35     InitializationSeedLocked = 5,
36     /// A lack of internal resources prevented the DPE from fulfilling the
37     /// command.
38     OutOfMemory = 6,
39     /// The command was canceled.
40     Canceled = 7,
41 }
42 
43 impl<E> From<minicbor::encode::Error<E>> for ErrCode {
from(_error: minicbor::encode::Error<E>) -> Self44     fn from(_error: minicbor::encode::Error<E>) -> Self {
45         error!("Failed to encode CBOR message");
46         ErrCode::InternalError
47     }
48 }
49 
50 impl From<minicbor::decode::Error> for ErrCode {
from(_error: minicbor::decode::Error) -> Self51     fn from(_error: minicbor::decode::Error) -> Self {
52         error!("Failed to decode CBOR message");
53         ErrCode::InvalidArgument
54     }
55 }
56 
57 impl From<core::num::TryFromIntError> for ErrCode {
from(_: core::num::TryFromIntError) -> Self58     fn from(_: core::num::TryFromIntError) -> Self {
59         error!("Unexpected failure: core::num::TryFromIntError");
60         ErrCode::InternalError
61     }
62 }
63 
64 impl From<u32> for ErrCode {
from(value: u32) -> Self65     fn from(value: u32) -> Self {
66         match value {
67             1 => Self::InternalError,
68             2 => Self::InvalidCommand,
69             3 => Self::InvalidArgument,
70             4 => Self::SessionExhausted,
71             5 => Self::InitializationSeedLocked,
72             6 => Self::OutOfMemory,
73             7 => Self::Canceled,
74             _ => {
75                 error!("Unknown error code");
76                 Self::InternalError
77             }
78         }
79     }
80 }
81 
82 /// A Result type using a DPE [`ErrCode`] error type.
83 pub type DpeResult<T> = Result<T, ErrCode>;
84