1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2023 Simo Sorce
// See LICENSE.txt file for terms
//! This module implements functions to manage errors
use std::error;
use std::fmt;
use crate::pkcs11::*;
use asn1;
use ossl;
use serde_json;
/// The Result type used within the project, wraps
/// errors via the custom Error enum
pub type Result<T> = std::result::Result<T, Error>;
/// The project's error object
#[derive(Debug)]
pub struct Error {
/// The error kind
kind: ErrorKind,
/// The originating error, if a mapping occurred
origin: Option<Box<dyn error::Error>>,
/// The error message string if set
errmsg: Option<String>,
/// Use only by ErrorKind::BufferTooSmall, indicates the
/// required buffer size if the function is called again
reqsize: usize,
/// The PKCS#11 CK_RV error code to be returned to the application
ckrv: CK_RV,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum ErrorKind {
/// A Cryptoki-style error, see ckrv Error field
CkError,
/// The attribute was not found, see errmsg
AttributeNotFound,
/// This error is used to indicate a provided buffer is too small
BufferTooSmall,
/// Other error, see origin
Nested,
}
macro_rules! trace_err {
($err:expr) => {{
let e = $err;
#[cfg(feature = "log")]
if e.ckrv != CKR_OK {
use log::error;
error!("{}", &e);
}
e
}};
}
impl Error {
/// Creates an error that represents a PKCS#11 Error code
pub fn ck_rv(ckrv: CK_RV) -> Error {
trace_err!(Error {
kind: ErrorKind::CkError,
origin: None,
errmsg: None,
reqsize: 0,
ckrv: ckrv,
})
}
/// Creates an error that represents a PKCS#11 Error code, and stores
/// the originating error code that was mapped to this error code
pub fn ck_rv_from_error<E>(ckrv: CK_RV, error: E) -> Error
where
E: Into<Box<dyn error::Error>> + std::fmt::Display,
{
trace_err!(Error {
kind: ErrorKind::CkError,
origin: Some(error.into()),
errmsg: None,
reqsize: 0,
ckrv: ckrv,
})
}
/// Creates an error that represents a PKCS#11 Error code, and includes
/// an error message
pub fn ck_rv_with_errmsg(ckrv: CK_RV, errmsg: String) -> Error {
trace_err!(Error {
kind: ErrorKind::CkError,
origin: None,
errmsg: Some(errmsg),
reqsize: 0,
ckrv: ckrv,
})
}
/// Creates an AttributeNotFound error, and includes an error message
pub fn not_found(errmsg: String) -> Error {
trace_err!(Error {
kind: ErrorKind::AttributeNotFound,
origin: None,
errmsg: Some(errmsg),
reqsize: 0,
ckrv: CKR_GENERAL_ERROR,
})
}
/// Creates an general (unspecified) error message from a previous error
pub fn other_error<E>(error: E) -> Error
where
E: Into<Box<dyn error::Error>> + std::fmt::Display,
{
trace_err!(Error {
kind: ErrorKind::Nested,
origin: Some(error.into()),
errmsg: None,
reqsize: 0,
ckrv: CKR_GENERAL_ERROR,
})
}
/// Creates a BufferTooSmall error and set the required buffer size
pub fn buf_too_small(reqsize: usize) -> Error {
trace_err!(Error {
kind: ErrorKind::BufferTooSmall,
origin: None,
errmsg: None,
reqsize: reqsize,
ckrv: CKR_BUFFER_TOO_SMALL,
})
}
/// Returns the error kind
pub fn kind(&self) -> ErrorKind {
self.kind
}
/// Check if this is a AttributeNotFound error
pub fn attr_not_found(&self) -> bool {
return self.kind == ErrorKind::AttributeNotFound;
}
/// Returns the associated PKCS#11 Error code
pub fn rv(&self) -> CK_RV {
self.ckrv
}
/// Returns the associated required buffer size
pub fn reqsize(&self) -> usize {
self.reqsize
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ret = match self.kind {
ErrorKind::CkError => write!(f, "Generic CK error"),
ErrorKind::AttributeNotFound => write!(f, "Attribute not found"),
ErrorKind::BufferTooSmall => write!(f, "Buffer too small"),
ErrorKind::Nested => write!(f, "Nested error"),
};
if ret.is_err() {
return ret;
}
let ret = write!(f, ", {}", ckrv_to_string(self.ckrv));
if ret.is_err() {
return ret;
}
match &self.origin {
Some(e) => {
let ret = write!(f, " - Error from: {{ {} }}", e);
if ret.is_err() {
return ret;
}
}
None => (),
}
match &self.errmsg {
Some(e) => {
let ret = write!(f, " - With message: {}", e);
if ret.is_err() {
return ret;
}
}
None => (),
}
if self.reqsize != 0 {
let ret = write!(f, " - With reqsize: {}", self.reqsize);
if ret.is_err() {
return ret;
}
}
Ok(())
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
/// Maps a Std IoError to a generic error
fn from(error: std::io::Error) -> Error {
Error::other_error(error)
}
}
impl From<serde_json::Error> for Error {
/// Maps a Serde Json Error to a generic error
fn from(error: serde_json::Error) -> Error {
Error::other_error(error)
}
}
impl From<std::num::TryFromIntError> for Error {
/// Maps an integer conversion error to a generic error
fn from(error: std::num::TryFromIntError) -> Error {
Error::other_error(error)
}
}
impl From<std::convert::Infallible> for Error {
/// Maps an infallible error to a generic error
fn from(error: std::convert::Infallible) -> Error {
Error::other_error(error)
}
}
impl From<CK_RV> for Error {
/// Maps a naked PKCS#11 Error code to an Error
fn from(error: CK_RV) -> Error {
Error::ck_rv(error)
}
}
impl From<asn1::ParseError> for Error {
/// Maps a ASN.1 parsing error to a generic error
fn from(error: asn1::ParseError) -> Error {
Error::other_error(error)
}
}
impl From<std::num::ParseIntError> for Error {
/// Maps an integer parsing error to a generic error
fn from(error: std::num::ParseIntError) -> Error {
Error::other_error(error)
}
}
impl From<asn1::WriteError> for Error {
/// Maps a write error to a generic error
fn from(error: asn1::WriteError) -> Error {
Error::other_error(error)
}
}
impl From<Vec<u8>> for Error {
/// Consumes a vector and generates a BufferTooSmall error
/// with its length as the requested length
fn from(v: Vec<u8>) -> Error {
Error::buf_too_small(v.len())
}
}
use std::array::TryFromSliceError;
impl From<TryFromSliceError> for Error {
/// Maps an error to manipulate a slice to a generic error
fn from(error: TryFromSliceError) -> Error {
Error::other_error(error)
}
}
impl From<ossl::Error> for Error {
/// Maps an openssl error
fn from(error: ossl::Error) -> Error {
match error.kind() {
ossl::ErrorKind::KeyError => Error::ck_rv(CKR_KEY_INDIGESTIBLE),
ossl::ErrorKind::WrapperError => Error::ck_rv(CKR_GENERAL_ERROR),
_ => Error::ck_rv(CKR_DEVICE_ERROR),
}
}
}