native_pkcs11_core/
lib.rs

1// Copyright 2022 Google LLC
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
15use attribute::AttributeType;
16use pkcs11_sys::*;
17use thiserror::Error;
18
19pub mod attribute;
20pub mod mechanism;
21pub mod object;
22
23pub type Result<T> = std::result::Result<T, Error>;
24
25#[derive(Error, Debug)]
26pub enum Error {
27    // Cryptoki errors.
28    #[error("arguments bad")]
29    ArgumentsBad,
30
31    #[error("{0} is not a valid attribute type")]
32    AttributeTypeInvalid(CK_ATTRIBUTE_TYPE),
33
34    #[error("the value for attribute {0} is invalid")]
35    AttributeValueInvalid(AttributeType),
36
37    #[error("buffer too small")]
38    BufferTooSmall,
39
40    #[error("cryptoki module has already been initialized")]
41    CryptokiAlreadyInitialized,
42
43    #[error("cryptoki module has not been initialized")]
44    CryptokiNotInitialized,
45
46    #[error("function not parallel")]
47    FunctionNotParallel,
48
49    #[error("function not supported")]
50    FunctionNotSupported,
51
52    #[error("key handle {0} is invalid")]
53    KeyHandleInvalid(CK_OBJECT_HANDLE),
54
55    #[error("module cannot function without being able to spawn threads")]
56    NeedToCreateThreads,
57
58    #[error("{0} is not a valid mechanism")]
59    MechanismInvalid(CK_MECHANISM_TYPE),
60
61    #[error("object {0} is invalid")]
62    ObjectHandleInvalid(CK_OBJECT_HANDLE),
63
64    #[error("operation has not been initialized")]
65    OperationNotInitialized,
66
67    #[error("no random number generator")]
68    RandomNoRng,
69
70    #[error("session handle {0} is invalid")]
71    SessionHandleInvalid(CK_SESSION_HANDLE),
72
73    #[error("token does not support parallel sessions")]
74    SessionParallelNotSupported,
75
76    #[error("slot id {0} is invalid")]
77    SlotIdInvalid(CK_SLOT_ID),
78
79    #[error("token is write protected")]
80    TokenWriteProtected,
81
82    // Other errors.
83    #[error("{0}")]
84    FromUtf8(#[from] std::string::FromUtf8Error),
85
86    #[error("{0}")]
87    FromVecWithNul(#[from] std::ffi::FromVecWithNulError),
88
89    #[error("null pointer error")]
90    NullPtr,
91
92    #[cfg(target_os = "macos")]
93    #[error("{0}")]
94    Pkcs11Keychain(#[from] native_pkcs11_keychain::Error),
95
96    #[error("{0}")]
97    TryFromInt(#[from] std::num::TryFromIntError),
98
99    #[error("{0}")]
100    TryFromSlice(#[from] std::array::TryFromSliceError),
101
102    // Catch-all for backend-related errors.
103    #[error("{0}")]
104    Backend(#[from] Box<dyn std::error::Error>),
105
106    #[error("{0}")]
107    Todo(String),
108}
109
110impl From<Error> for CK_RV {
111    fn from(e: Error) -> Self {
112        match e {
113            Error::ArgumentsBad => CKR_ARGUMENTS_BAD,
114            Error::AttributeTypeInvalid(_) => CKR_ATTRIBUTE_TYPE_INVALID,
115            Error::AttributeValueInvalid(_) => CKR_ATTRIBUTE_VALUE_INVALID,
116            Error::BufferTooSmall => CKR_BUFFER_TOO_SMALL,
117            Error::CryptokiAlreadyInitialized => CKR_CRYPTOKI_ALREADY_INITIALIZED,
118            Error::CryptokiNotInitialized => CKR_CRYPTOKI_NOT_INITIALIZED,
119            Error::FunctionNotParallel => CKR_FUNCTION_NOT_PARALLEL,
120            Error::FunctionNotSupported => CKR_FUNCTION_NOT_SUPPORTED,
121            Error::KeyHandleInvalid(_) => CKR_KEY_HANDLE_INVALID,
122            Error::MechanismInvalid(_) => CKR_MECHANISM_INVALID,
123            Error::NeedToCreateThreads => CKR_NEED_TO_CREATE_THREADS,
124            Error::ObjectHandleInvalid(_) => CKR_OBJECT_HANDLE_INVALID,
125            Error::OperationNotInitialized => CKR_OPERATION_NOT_INITIALIZED,
126            Error::RandomNoRng => CKR_RANDOM_NO_RNG,
127            Error::SessionHandleInvalid(_) => CKR_SESSION_HANDLE_INVALID,
128            Error::SessionParallelNotSupported => CKR_SESSION_PARALLEL_NOT_SUPPORTED,
129            Error::SlotIdInvalid(_) => CKR_SLOT_ID_INVALID,
130            Error::TokenWriteProtected => CKR_TOKEN_WRITE_PROTECTED,
131
132            Error::Backend(_)
133            | Error::FromUtf8(_)
134            | Error::FromVecWithNul(_)
135            | Error::NullPtr
136            | Error::Todo(_)
137            | Error::TryFromInt(_)
138            | Error::TryFromSlice(_) => CKR_GENERAL_ERROR,
139
140            #[cfg(target_os = "macos")]
141            Error::Pkcs11Keychain(_) => CKR_GENERAL_ERROR,
142        }
143    }
144}