tss_esapi/
error.rs

1// Copyright 2020 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::constants::response_code::Tss2ResponseCode;
4use crate::tss2_esys::TSS2_RC;
5/// Main error type used by the crate to return issues with a method call. The value can either be
6/// a TSS-generated response code or a wrapper error - marking an issue caught within the wrapping
7/// layer.
8pub type Result<T> = std::result::Result<T, Error>;
9
10#[derive(Copy, Clone, PartialEq, Eq, Debug)]
11pub enum Error {
12    WrapperError(WrapperErrorKind),
13    Tss2Error(Tss2ResponseCode),
14}
15
16impl Error {
17    pub(crate) fn from_tss_rc(rc: TSS2_RC) -> Self {
18        Error::Tss2Error(Tss2ResponseCode::from_tss_rc(rc))
19    }
20
21    pub(crate) fn local_error(kind: WrapperErrorKind) -> Self {
22        Error::WrapperError(kind)
23    }
24
25    /// Verify whether the value contained is a success response code.
26    pub fn is_success(self) -> bool {
27        if let Error::Tss2Error(tss2_rc) = self {
28            tss2_rc.is_success()
29        } else {
30            false
31        }
32    }
33}
34
35impl std::fmt::Display for Error {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            Error::WrapperError(e) => e.fmt(f),
39            Error::Tss2Error(e) => e.fmt(f),
40        }
41    }
42}
43
44impl std::error::Error for Error {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            Error::WrapperError(wrapper_error) => Some(wrapper_error),
48            Error::Tss2Error(response_code) => Some(response_code),
49        }
50    }
51}
52
53/// List of error types that might occur in the wrapper.
54#[derive(Copy, Clone, PartialEq, Eq, Debug)]
55pub enum WrapperErrorKind {
56    /// Returned when a size or length-defined parameter does not conform with the size
57    /// restrictions for it.
58    WrongParamSize,
59    /// Returned when a required parameter was not passed, usually to a builder.
60    ParamsMissing,
61    /// Returned when two or more parameters have inconsistent values or variants.
62    InconsistentParams,
63    /// Returned when the value of a parameter is not yet supported.
64    UnsupportedParam,
65    /// Returned when the value of a parameter is invalid for that type.
66    InvalidParam,
67    /// Returned when the TPM returns an invalid value from a call.
68    WrongValueFromTpm,
69    /// Returned when a session for authentication has not been set
70    /// before the call is made.
71    MissingAuthSession,
72    /// Returned when a handle is required to be in a specific state
73    /// (i.g. Open, Flushed, Closed) but it is not.
74    InvalidHandleState,
75    /// An unexpected internal error occurred.
76    InternalError,
77}
78
79impl std::fmt::Display for WrapperErrorKind {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        match self {
82            WrapperErrorKind::WrongParamSize => {
83                write!(f, "parameter provided is of the wrong size")
84            }
85            WrapperErrorKind::ParamsMissing => {
86                write!(f, "some of the required parameters were not provided")
87            }
88            WrapperErrorKind::InconsistentParams => write!(
89                f,
90                "the provided parameters have inconsistent values or variants"
91            ),
92            WrapperErrorKind::UnsupportedParam => write!(
93                f,
94                "the provided parameter is not yet supported by the library"
95            ),
96            WrapperErrorKind::InvalidParam => {
97                write!(f, "the provided parameter is invalid for that type.")
98            }
99            WrapperErrorKind::WrongValueFromTpm => write!(f, "the TPM returned an invalid value."),
100            WrapperErrorKind::MissingAuthSession => write!(f, "Missing authorization session"),
101            WrapperErrorKind::InvalidHandleState => write!(f, "Invalid handle state"),
102            WrapperErrorKind::InternalError => {
103                write!(f, "an unexpected error occurred within the crate")
104            }
105        }
106    }
107}
108
109impl std::error::Error for WrapperErrorKind {}