Skip to content

Commit

Permalink
moving errors into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
mheese committed Nov 12, 2017
1 parent 7854f3f commit ff1970a
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
[package]
name = "pkcs11"
version = "0.1.0"
version = "0.2.0"
authors = ["Marcus Heese <[email protected]>"]
description = "Rust PKCS#11 Library"
#documentation = "https://github.com/mheese/rust-pkcs11"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Here is a list of the implementation status and plans on what to do next:
- [x] Initializing and Dropping PKCS#11 context
- [x] Implementing Token and PIN Management functions
- [x] Implementing Session Management functions
- [ ] Implementing Object Management functions
- [x] Implementing Object Management functions
- [ ] Implementing Key Management functions
- [ ] Implementing Encryption/Decryption functions
- [ ] Implementing Message Digest functions
Expand All @@ -39,6 +39,7 @@ Here is a list of the implementation status and plans on what to do next:
- [x] Reorganize code of low-level API (too bloated, which we all know is what PKCS#11 is like)
- [x] Import the rest of the C header `pkcs11t.h` types into rust
- [ ] Import the rust of the C header `pkcs11f.h` functions into rust
- [ ] C type constants to string converter functions, and the reverse
- [ ] Design and implement high-level API
- [x] Publish on crates.io (wow, that was easy)
- [ ] Write and Generate Documentation for Rust docs
Expand Down
13 changes: 13 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Copyright 2017 Marcus Heese
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
verbose = false
disable_all_formatting = false
skip_children = false
Expand Down
142 changes: 142 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2017 Marcus Heese
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std;
use types::*;

#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Module(&'static str),
InvalidInput(&'static str),
Pkcs11(CK_RV),
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::Io(err)
}
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Error::Io(ref err) => write!(f, "IO: {}", err),
Error::Module(ref err) => write!(f, "PKCS#11 Module: {}", err),
Error::InvalidInput(ref err) => write!(f, "PKCS#11 Invalid Input: {}", err),
Error::Pkcs11(ref err) => write!(f, "PKCS#11: {} (0x{:x})", strerror(*err), err),
}
}
}

fn strerror(err: CK_RV) -> &'static str {
match err {
CKR_OK => "CKR_OK",
CKR_CANCEL => "CKR_CANCEL",
CKR_HOST_MEMORY => "CKR_HOST_MEMORY",
CKR_SLOT_ID_INVALID => "CKR_SLOT_ID_INVALID",
CKR_GENERAL_ERROR => "CKR_GENERAL_ERROR",
CKR_FUNCTION_FAILED => "CKR_FUNCTION_FAILED",
CKR_ARGUMENTS_BAD => "CKR_ARGUMENTS_BAD",
CKR_NO_EVENT => "CKR_NO_EVENT",
CKR_NEED_TO_CREATE_THREADS => "CKR_NEED_TO_CREATE_THREADS",
CKR_CANT_LOCK => "CKR_CANT_LOCK",
CKR_ATTRIBUTE_READ_ONLY => "CKR_ATTRIBUTE_READ_ONLY",
CKR_ATTRIBUTE_SENSITIVE => "CKR_ATTRIBUTE_SENSITIVE",
CKR_ATTRIBUTE_TYPE_INVALID => "CKR_ATTRIBUTE_TYPE_INVALID",
CKR_ATTRIBUTE_VALUE_INVALID => "CKR_ATTRIBUTE_VALUE_INVALID",
CKR_ACTION_PROHIBITED => "CKR_ACTION_PROHIBITED",
CKR_DATA_INVALID => "CKR_DATA_INVALID",
CKR_DATA_LEN_RANGE => "CKR_DATA_LEN_RANGE",
CKR_DEVICE_ERROR => "CKR_DEVICE_ERROR",
CKR_DEVICE_MEMORY => "CKR_DEVICE_MEMORY",
CKR_DEVICE_REMOVED => "CKR_DEVICE_REMOVED",
CKR_ENCRYPTED_DATA_INVALID => "CKR_ENCRYPTED_DATA_INVALID",
CKR_ENCRYPTED_DATA_LEN_RANGE => "CKR_ENCRYPTED_DATA_LEN_RANGE",
CKR_FUNCTION_CANCELED => "CKR_FUNCTION_CANCELED",
CKR_FUNCTION_NOT_PARALLEL => "CKR_FUNCTION_NOT_PARALLEL",
CKR_FUNCTION_NOT_SUPPORTED => "CKR_FUNCTION_NOT_SUPPORTED",
CKR_KEY_HANDLE_INVALID => "CKR_KEY_HANDLE_INVALID",
CKR_KEY_SIZE_RANGE => "CKR_KEY_SIZE_RANGE",
CKR_KEY_TYPE_INCONSISTENT => "CKR_KEY_TYPE_INCONSISTENT",
CKR_KEY_NOT_NEEDED => "CKR_KEY_NOT_NEEDED",
CKR_KEY_CHANGED => "CKR_KEY_CHANGED",
CKR_KEY_NEEDED => "CKR_KEY_NEEDED",
CKR_KEY_INDIGESTIBLE => "CKR_KEY_INDIGESTIBLE",
CKR_KEY_FUNCTION_NOT_PERMITTED => "CKR_KEY_FUNCTION_NOT_PERMITTED",
CKR_KEY_NOT_WRAPPABLE => "CKR_KEY_NOT_WRAPPABLE",
CKR_KEY_UNEXTRACTABLE => "CKR_KEY_UNEXTRACTABLE",
CKR_MECHANISM_INVALID => "CKR_MECHANISM_INVALID",
CKR_MECHANISM_PARAM_INVALID => "CKR_MECHANISM_PARAM_INVALID",
CKR_OBJECT_HANDLE_INVALID => "CKR_OBJECT_HANDLE_INVALID",
CKR_OPERATION_ACTIVE => "CKR_OPERATION_ACTIVE",
CKR_OPERATION_NOT_INITIALIZED => "CKR_OPERATION_NOT_INITIALIZED",
CKR_PIN_INCORRECT => "CKR_PIN_INCORRECT",
CKR_PIN_INVALID => "CKR_PIN_INVALID",
CKR_PIN_LEN_RANGE => "CKR_PIN_LEN_RANGE",
CKR_PIN_EXPIRED => "CKR_PIN_EXPIRED",
CKR_PIN_LOCKED => "CKR_PIN_LOCKED",
CKR_SESSION_CLOSED => "CKR_SESSION_CLOSED",
CKR_SESSION_COUNT => "CKR_SESSION_COUNT",
CKR_SESSION_HANDLE_INVALID => "CKR_SESSION_HANDLE_INVALID",
CKR_SESSION_PARALLEL_NOT_SUPPORTED => "CKR_SESSION_PARALLEL_NOT_SUPPORTED",
CKR_SESSION_READ_ONLY => "CKR_SESSION_READ_ONLY",
CKR_SESSION_EXISTS => "CKR_SESSION_EXISTS",
CKR_SESSION_READ_ONLY_EXISTS => "CKR_SESSION_READ_ONLY_EXISTS",
CKR_SESSION_READ_WRITE_SO_EXISTS => "CKR_SESSION_READ_WRITE_SO_EXISTS",
CKR_SIGNATURE_INVALID => "CKR_SIGNATURE_INVALID",
CKR_SIGNATURE_LEN_RANGE => "CKR_SIGNATURE_LEN_RANGE",
CKR_TEMPLATE_INCOMPLETE => "CKR_TEMPLATE_INCOMPLETE",
CKR_TEMPLATE_INCONSISTENT => "CKR_TEMPLATE_INCONSISTENT",
CKR_TOKEN_NOT_PRESENT => "CKR_TOKEN_NOT_PRESENT",
CKR_TOKEN_NOT_RECOGNIZED => "CKR_TOKEN_NOT_RECOGNIZED",
CKR_TOKEN_WRITE_PROTECTED => "CKR_TOKEN_WRITE_PROTECTED",
CKR_UNWRAPPING_KEY_HANDLE_INVALID => "CKR_UNWRAPPING_KEY_HANDLE_INVALID",
CKR_UNWRAPPING_KEY_SIZE_RANGE => "CKR_UNWRAPPING_KEY_SIZE_RANGE",
CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT => "CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT",
CKR_USER_ALREADY_LOGGED_IN => "CKR_USER_ALREADY_LOGGED_IN",
CKR_USER_NOT_LOGGED_IN => "CKR_USER_NOT_LOGGED_IN",
CKR_USER_PIN_NOT_INITIALIZED => "CKR_USER_PIN_NOT_INITIALIZED",
CKR_USER_TYPE_INVALID => "CKR_USER_TYPE_INVALID",
CKR_USER_ANOTHER_ALREADY_LOGGED_IN => "CKR_USER_ANOTHER_ALREADY_LOGGED_IN",
CKR_USER_TOO_MANY_TYPES => "CKR_USER_TOO_MANY_TYPES",
CKR_WRAPPED_KEY_INVALID => "CKR_WRAPPED_KEY_INVALID",
CKR_WRAPPED_KEY_LEN_RANGE => "CKR_WRAPPED_KEY_LEN_RANGE",
CKR_WRAPPING_KEY_HANDLE_INVALID => "CKR_WRAPPING_KEY_HANDLE_INVALID",
CKR_WRAPPING_KEY_SIZE_RANGE => "CKR_WRAPPING_KEY_SIZE_RANGE",
CKR_WRAPPING_KEY_TYPE_INCONSISTENT => "CKR_WRAPPING_KEY_TYPE_INCONSISTENT",
CKR_RANDOM_SEED_NOT_SUPPORTED => "CKR_RANDOM_SEED_NOT_SUPPORTED",
CKR_RANDOM_NO_RNG => "CKR_RANDOM_NO_RNG",
CKR_DOMAIN_PARAMS_INVALID => "CKR_DOMAIN_PARAMS_INVALID",
CKR_CURVE_NOT_SUPPORTED => "CKR_CURVE_NOT_SUPPORTED",
CKR_BUFFER_TOO_SMALL => "CKR_BUFFER_TOO_SMALL",
CKR_SAVED_STATE_INVALID => "CKR_SAVED_STATE_INVALID",
CKR_INFORMATION_SENSITIVE => "CKR_INFORMATION_SENSITIVE",
CKR_STATE_UNSAVEABLE => "CKR_STATE_UNSAVEABLE",
CKR_CRYPTOKI_NOT_INITIALIZED => "CKR_CRYPTOKI_NOT_INITIALIZED",
CKR_CRYPTOKI_ALREADY_INITIALIZED => "CKR_CRYPTOKI_ALREADY_INITIALIZED",
CKR_MUTEX_BAD => "CKR_MUTEX_BAD",
CKR_MUTEX_NOT_LOCKED => "CKR_MUTEX_NOT_LOCKED",
CKR_NEW_PIN_MODE => "CKR_NEW_PIN_MODE",
CKR_NEXT_OTP => "CKR_NEXT_OTP",
CKR_EXCEEDED_MAX_ITERATIONS => "CKR_EXCEEDED_MAX_ITERATIONS",
CKR_FIPS_SELF_TEST_FAILED => "CKR_FIPS_SELF_TEST_FAILED",
CKR_LIBRARY_LOAD_FAILED => "CKR_LIBRARY_LOAD_FAILED",
CKR_PIN_TOO_WEAK => "CKR_PIN_TOO_WEAK",
CKR_PUBLIC_KEY_INVALID => "CKR_PUBLIC_KEY_INVALID",
CKR_FUNCTION_REJECTED => "CKR_FUNCTION_REJECTED",
CKR_VENDOR_DEFINED => "CKR_VENDOR_DEFINED",
_ => "unknown",
}
}
28 changes: 3 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ mod tests;
pub mod types;
/// This module is basically a full conversion of the `pkcs11f.h` C header file.
pub mod functions;
/// The error types are defined here - they are used throughout the crate.
pub mod errors;

use types::*;
use functions::*;
use errors::Error;


use std::mem;
Expand Down Expand Up @@ -56,31 +59,6 @@ impl CkFrom<CK_BBOOL> for bool {
}
}

#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Module(&'static str),
InvalidInput(&'static str),
Pkcs11(CK_RV),
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::Io(err)
}
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Error::Io(ref err) => write!(f, "IO error: {}", err),
Error::Module(ref err) => write!(f, "PKCS#11 Module error: {}", err),
Error::InvalidInput(ref err) => write!(f, "Invalid Input for PKCS#11: {}", err),
Error::Pkcs11(ref err) => write!(f, "PKCS#11 error: 0x{:x}", err),
}
}
}

fn label_from_str(label: &str) -> [CK_UTF8CHAR; 32] {
// initialize a fixed-size array with whitespace characters
let mut lab: [CK_UTF8CHAR; 32] = [32; 32];
Expand Down
1 change: 1 addition & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tests::num_traits::Num;

use super::*;
use super::types::*;
use super::errors::Error;
use num_bigint::BigUint;

const PKCS11_MODULE_FILENAME: &'static str = "/usr/local/lib/softhsm/libsofthsm2.so";
Expand Down

0 comments on commit ff1970a

Please sign in to comment.