aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-17 12:47:52 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-20 21:08:50 +0000
commit5e258d26b55af6bed7c316b1c7ac12e20946702d (patch)
treefee2be71586bef2f1978eb1cf40ef84c8ac6492b /src
parent944e1fa0d51e547dde2a9368d2b8431b109f63c4 (diff)
downloadnitrokey-rs-5e258d26b55af6bed7c316b1c7ac12e20946702d.tar.gz
nitrokey-rs-5e258d26b55af6bed7c316b1c7ac12e20946702d.tar.bz2
Refactor library errors into LibraryError enum
Previously, library errors were part of the CommandError enum. As command errors and library errors are two different error types, they should be split into two enums.
Diffstat (limited to 'src')
-rw-r--r--src/auth.rs6
-rw-r--r--src/config.rs4
-rw-r--r--src/device.rs22
-rw-r--r--src/error.rs74
-rw-r--r--src/lib.rs2
-rw-r--r--src/otp.rs20
-rw-r--r--src/pws.rs14
-rw-r--r--src/util.rs4
8 files changed, 89 insertions, 57 deletions
diff --git a/src/auth.rs b/src/auth.rs
index e05f6b3..d1eb049 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -55,7 +55,7 @@ pub trait Authenticate {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`RngError`]: enum.CommandError.html#variant.RngError
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn authenticate_user(self, password: &str) -> Result<User<Self>, (Self, Error)>
@@ -101,7 +101,7 @@ pub trait Authenticate {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`RngError`]: enum.CommandError.html#variant.RngError
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn authenticate_admin(self, password: &str) -> Result<Admin<Self>, (Self, Error)>
@@ -287,7 +287,7 @@ impl<T: Device> Admin<T> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
pub fn write_config(&self, config: Config) -> Result<(), Error> {
let raw_config = RawConfig::try_from(config)?;
unsafe {
diff --git a/src/config.rs b/src/config.rs
index 741d67e..6aa6d10 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,4 +1,4 @@
-use crate::error::{CommandError, Error};
+use crate::error::{Error, LibraryError};
/// The configuration for a Nitrokey.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -41,7 +41,7 @@ fn option_to_config_otp_slot(value: Option<u8>) -> Result<u8, Error> {
if value < 3 {
Ok(value)
} else {
- Err(CommandError::InvalidSlot.into())
+ Err(LibraryError::InvalidSlot.into())
}
}
None => Ok(255),
diff --git a/src/device.rs b/src/device.rs
index ccd0597..5c4014b 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -461,7 +461,7 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn change_admin_pin(&self, current: &str, new: &str) -> Result<(), Error> {
let current_string = get_cstring(current)?;
@@ -497,7 +497,7 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn change_user_pin(&self, current: &str, new: &str) -> Result<(), Error> {
let current_string = get_cstring(current)?;
@@ -533,7 +533,7 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn unlock_user_pin(&self, admin_pin: &str, user_pin: &str) -> Result<(), Error> {
let admin_pin_string = get_cstring(admin_pin)?;
@@ -867,7 +867,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn change_update_pin(&self, current: &str, new: &str) -> Result<(), Error> {
let current_string = get_cstring(current)?;
@@ -907,7 +907,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn enable_firmware_update(&self, update_pin: &str) -> Result<(), Error> {
let update_pin_string = get_cstring(update_pin)?;
@@ -943,7 +943,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn enable_encrypted_volume(&self, user_pin: &str) -> Result<(), Error> {
let user_pin = get_cstring(user_pin)?;
@@ -1021,7 +1021,7 @@ impl Storage {
///
/// [`enable_encrypted_volume`]: #method.enable_encrypted_volume
/// [`AesDecryptionFailed`]: enum.CommandError.html#variant.AesDecryptionFailed
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
pub fn enable_hidden_volume(&self, volume_password: &str) -> Result<(), Error> {
let volume_password = get_cstring(volume_password)?;
unsafe {
@@ -1099,7 +1099,7 @@ impl Storage {
/// ```
///
/// [`AesDecryptionFailed`]: enum.CommandError.html#variant.AesDecryptionFailed
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
pub fn create_hidden_volume(
&self,
slot: u8,
@@ -1145,7 +1145,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn set_unencrypted_volume_mode(
&self,
@@ -1276,7 +1276,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn clear_new_sd_card_warning(&self, admin_pin: &str) -> Result<(), Error> {
let admin_pin = get_cstring(admin_pin)?;
@@ -1304,7 +1304,7 @@ impl Storage {
/// - [`InvalidString`][] if one of the provided passwords contains a null byte
/// - [`WrongPassword`][] if the admin password is wrong
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn export_firmware(&self, admin_pin: &str) -> Result<(), Error> {
let admin_pin_string = get_cstring(admin_pin)?;
diff --git a/src/error.rs b/src/error.rs
index c5a975e..f40d07f 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -11,6 +11,8 @@ pub enum Error {
CommandError(CommandError),
/// Placeholder for testing.
CommunicationError(CommunicationError),
+ /// A library usage error.
+ LibraryError(LibraryError),
/// An error that occured during random number generation.
RandError(rand_core::Error),
/// An unknown error returned by libnitrokey.
@@ -21,6 +23,8 @@ impl From<raw::c_int> for Error {
fn from(code: raw::c_int) -> Self {
if let Some(err) = CommandError::try_from(code) {
Error::CommandError(err)
+ } else if let Some(err) = LibraryError::try_from(code) {
+ Error::LibraryError(err)
} else {
Error::Unknown(code.into())
}
@@ -33,6 +37,12 @@ impl From<CommandError> for Error {
}
}
+impl From<LibraryError> for Error {
+ fn from(err: LibraryError) -> Self {
+ Error::LibraryError(err)
+ }
+}
+
impl From<rand_core::Error> for Error {
fn from(error: rand_core::Error) -> Self {
Error::RandError(error)
@@ -44,6 +54,7 @@ impl error::Error for Error {
match *self {
Error::CommandError(ref err) => Some(err),
Error::CommunicationError(_) => None,
+ Error::LibraryError(ref err) => Some(err),
Error::RandError(ref err) => Some(err),
Error::Unknown(_) => None,
}
@@ -55,6 +66,7 @@ impl fmt::Display for Error {
match *self {
Error::CommandError(ref err) => write!(f, "Command error: {}", err),
Error::CommunicationError(_) => write!(f, "Placeholder"),
+ Error::LibraryError(ref err) => write!(f, "Library error: {}", err),
Error::RandError(ref err) => write!(f, "RNG error: {}", err),
Error::Unknown(ref err) => write!(f, "Unknown error: {}", err),
}
@@ -90,16 +102,6 @@ pub enum CommandError {
AesDecryptionFailed,
/// An unspecified error occurred.
Undefined,
- /// You passed a string containing a null byte.
- InvalidString,
- /// A supplied string exceeded a length limit.
- StringTooLong,
- /// You passed an invalid slot.
- InvalidSlot,
- /// The supplied string was not in hexadecimal format.
- InvalidHexString,
- /// The target buffer was smaller than the source.
- TargetBufferTooSmall,
}
/// Placeholder for testing.
@@ -122,10 +124,6 @@ impl CommandError {
8 => Some(CommandError::NotSupported),
9 => Some(CommandError::UnknownCommand),
10 => Some(CommandError::AesDecryptionFailed),
- 200 => Some(CommandError::StringTooLong),
- 201 => Some(CommandError::InvalidSlot),
- 202 => Some(CommandError::InvalidHexString),
- 203 => Some(CommandError::TargetBufferTooSmall),
_ => None,
}
}
@@ -149,13 +147,6 @@ impl CommandError {
CommandError::UnknownCommand => "This command is unknown".into(),
CommandError::AesDecryptionFailed => "AES decryption failed".into(),
CommandError::Undefined => "An unspecified error occurred".into(),
- CommandError::InvalidString => "You passed a string containing a null byte".into(),
- CommandError::StringTooLong => "The supplied string is too long".into(),
- CommandError::InvalidSlot => "The given slot is invalid".into(),
- CommandError::InvalidHexString => {
- "The supplied string is not in hexadecimal format".into()
- }
- CommandError::TargetBufferTooSmall => "The target buffer is too small".into(),
}
}
}
@@ -167,3 +158,44 @@ impl fmt::Display for CommandError {
write!(f, "{}", self.as_str())
}
}
+
+/// A library usage error.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum LibraryError {
+ /// A supplied string exceeded a length limit.
+ StringTooLong,
+ /// You passed an invalid slot.
+ InvalidSlot,
+ /// The supplied string was not in hexadecimal format.
+ InvalidHexString,
+ /// The target buffer was smaller than the source.
+ TargetBufferTooSmall,
+ /// You passed a string containing a null byte.
+ InvalidString,
+}
+
+impl LibraryError {
+ fn try_from(value: raw::c_int) -> Option<Self> {
+ match value {
+ 200 => Some(LibraryError::StringTooLong),
+ 201 => Some(LibraryError::InvalidSlot),
+ 202 => Some(LibraryError::InvalidHexString),
+ 203 => Some(LibraryError::TargetBufferTooSmall),
+ _ => None,
+ }
+ }
+}
+
+impl error::Error for LibraryError {}
+
+impl fmt::Display for LibraryError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.write_str(match *self {
+ LibraryError::StringTooLong => "The supplied string is too long",
+ LibraryError::InvalidSlot => "The given slot is invalid",
+ LibraryError::InvalidHexString => "The supplied string is not in hexadecimal format",
+ LibraryError::TargetBufferTooSmall => "The target buffer is too small",
+ LibraryError::InvalidString => "You passed a string containing a null byte",
+ })
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 8522e83..993ec92 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -104,7 +104,7 @@ pub use crate::device::{
connect, connect_model, Device, DeviceWrapper, Model, Pro, SdCardData, Storage,
StorageProductionInfo, StorageStatus, VolumeMode, VolumeStatus,
};
-pub use crate::error::{CommandError, CommunicationError, Error, Result};
+pub use crate::error::{CommandError, CommunicationError, Error, LibraryError, Result};
pub use crate::otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData};
pub use crate::pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT};
pub use crate::util::LogLevel;
diff --git a/src/otp.rs b/src/otp.rs
index 5dfe8b1..7535a77 100644
--- a/src/otp.rs
+++ b/src/otp.rs
@@ -47,8 +47,8 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`NoName`]: enum.CommandError.html#variant.NoName
fn write_hotp_slot(&self, data: OtpSlotData, counter: u64) -> Result<(), Error>;
@@ -83,8 +83,8 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`NoName`]: enum.CommandError.html#variant.NoName
fn write_totp_slot(&self, data: OtpSlotData, time_window: u16) -> Result<(), Error>;
@@ -115,7 +115,7 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
fn erase_hotp_slot(&self, slot: u8) -> Result<(), Error>;
/// Erases a TOTP slot.
@@ -145,7 +145,7 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
fn erase_totp_slot(&self, slot: u8) -> Result<(), Error>;
}
@@ -216,7 +216,7 @@ pub trait GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
fn get_hotp_slot_name(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_hotp_slot_name(slot)) }
@@ -245,7 +245,7 @@ pub trait GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
fn get_totp_slot_name(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_totp_slot_name(slot)) }
@@ -275,7 +275,7 @@ pub trait GenerateOtp {
/// ```
///
/// [`get_config`]: trait.Device.html#method.get_config
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`NotAuthorized`]: enum.CommandError.html#variant.NotAuthorized
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
fn get_hotp_code(&self, slot: u8) -> Result<String, Error> {
@@ -320,7 +320,7 @@ pub trait GenerateOtp {
///
/// [`set_time`]: #method.set_time
/// [`get_config`]: trait.Device.html#method.get_config
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`NotAuthorized`]: enum.CommandError.html#variant.NotAuthorized
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
fn get_totp_code(&self, slot: u8) -> Result<String, Error> {
diff --git a/src/pws.rs b/src/pws.rs
index e974737..47965d7 100644
--- a/src/pws.rs
+++ b/src/pws.rs
@@ -109,7 +109,7 @@ pub trait GetPasswordSafe {
/// [`lock`]: trait.Device.html#method.lock
/// [`AesDecryptionFailed`]: enum.CommandError.html#variant.AesDecryptionFailed
/// [`Device::build_aes_key`]: trait.Device.html#method.build_aes_key
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`Unknown`]: enum.CommandError.html#variant.Unknown
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, Error>;
@@ -207,7 +207,7 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
pub fn get_slot_name(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_name(slot)) }
@@ -240,7 +240,7 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
pub fn get_slot_login(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_login(slot)) }
@@ -273,7 +273,7 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
pub fn get_slot_password(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_password(slot)) }
@@ -304,8 +304,8 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
pub fn write_slot(
&self,
slot: u8,
@@ -350,7 +350,7 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
pub fn erase_slot(&self, slot: u8) -> Result<(), Error> {
unsafe { get_command_result(nitrokey_sys::NK_erase_password_safe_slot(slot)) }
}
diff --git a/src/util.rs b/src/util.rs
index 3b9904f..2738fce 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -5,7 +5,7 @@ use libc::{c_void, free};
use rand_core::RngCore;
use rand_os::OsRng;
-use crate::error::{CommandError, Error};
+use crate::error::{CommandError, Error, LibraryError};
/// Log level for libnitrokey.
///
@@ -78,7 +78,7 @@ pub fn generate_password(length: usize) -> Result<Vec<u8>, Error> {
}
pub fn get_cstring<T: Into<Vec<u8>>>(s: T) -> Result<CString, Error> {
- CString::new(s).or(Err(CommandError::InvalidString.into()))
+ CString::new(s).or(Err(LibraryError::InvalidString.into()))
}
impl Into<i32> for LogLevel {