oxidize: Fix second pass of compile errors and commented-out code

This commit gets the Rust code to compile! 🎉

Additionally, it fixes all of the commented out code that was failing
translation from C due to the use of unions, namely around the APDU
messages.

It does a fair amount of reformatting around branches, with the net
result hopefully being something actually a bit closer to the C code,
and a straightforward list of `if` statements.

It also removes all of the remaining externs that aren't supposed to be
externs, replacing them with a more straightforward usage of the module
system.

Finally it fixes all errors and warnings (relating to e.g. usage of
uninitialized memory), in addition to most clippy lints! (some have
been explicitly disabled)

All that said, it still doesn't do anything: it needs to be wired up to
a PCSC library first before that will be possible. But hey, it compiles!
This commit is contained in:
Tony Arcieri
2019-11-17 09:02:18 -08:00
parent 1d86885ab1
commit 7d40a9917e
10 changed files with 5366 additions and 6323 deletions
+96 -68
View File
@@ -1,3 +1,5 @@
//! Error types
// Adapted from yubico-piv-tool:
// <https://github.com/Yubico/yubico-piv-tool/>
//
@@ -30,73 +32,109 @@
use std::fmt;
/// Kinds of errors
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[allow(non_camel_case_types)]
pub enum ErrorKind {
YKPIV_OK = 0,
YKPIV_MEMORY_ERROR = -1,
YKPIV_PCSC_ERROR = -2,
YKPIV_SIZE_ERROR = -3,
YKPIV_APPLET_ERROR = -4,
YKPIV_AUTHENTICATION_ERROR = -5,
YKPIV_RANDOMNESS_ERROR = -6,
YKPIV_GENERIC_ERROR = -7,
YKPIV_KEY_ERROR = -8,
YKPIV_PARSE_ERROR = -9,
YKPIV_WRONG_PIN = -10,
YKPIV_INVALID_OBJECT = -11,
YKPIV_ALGORITHM_ERROR = -12,
YKPIV_PIN_LOCKED = -13,
YKPIV_ARGUMENT_ERROR = -14,
YKPIV_RANGE_ERROR = -15,
YKPIV_NOT_SUPPORTED = -16,
/// OK
// TODO(tarcieri): replace this with proper result types
Ok,
/// Memory error
MemoryError,
/// PCSC error
PcscError,
/// Size error
SizeError,
/// Applet error
AppletError,
/// Authentication error
AuthenticationError,
/// Randomness error
RandomnessError,
/// Generic error
GenericError,
/// Key error
KeyError,
/// Parse error
ParseError,
/// Wrong PIN
WrongPin,
/// Invalid object
InvalidObject,
/// Algorithm error
AlgorithmError,
/// PIN locked
PinLocked,
/// Argument error
ArgumentError,
/// Range error
RangeError,
/// Not supported
NotSupported,
}
impl ErrorKind {
/// Name of the error
/// Name of the error.
///
/// These names map to the legacy names from the Yubico C library, to
/// assist in web searches for relevant information for these errors.
pub fn name(self) -> &'static str {
match self {
ErrorKind::YKPIV_OK => "YKPIV_OK",
ErrorKind::YKPIV_MEMORY_ERROR => "YKPIV_MEMORY_ERROR",
ErrorKind::YKPIV_PCSC_ERROR => "YKPIV_PCSC_ERROR",
ErrorKind::YKPIV_SIZE_ERROR => "YKPIV_SIZE_ERROR",
ErrorKind::YKPIV_APPLET_ERROR => "YKPIV_APPLET_ERROR",
ErrorKind::YKPIV_AUTHENTICATION_ERROR => "YKPIV_AUTHENTICATION_ERROR",
ErrorKind::YKPIV_RANDOMNESS_ERROR => "YKPIV_RANDOMNESS_ERROR",
ErrorKind::YKPIV_GENERIC_ERROR => "YKPIV_GENERIC_ERROR",
ErrorKind::YKPIV_KEY_ERROR => "YKPIV_KEY_ERROR",
ErrorKind::YKPIV_PARSE_ERROR => "YKPIV_PARSE_ERROR",
ErrorKind::YKPIV_WRONG_PIN => "YKPIV_WRONG_PIN",
ErrorKind::YKPIV_INVALID_OBJECT => "YKPIV_INVALID_OBJECT",
ErrorKind::YKPIV_ALGORITHM_ERROR => "YKPIV_ALGORITHM_ERROR",
ErrorKind::YKPIV_PIN_LOCKED => "YKPIV_PIN_LOCKED",
ErrorKind::YKPIV_ARGUMENT_ERROR => "YKPIV_ARGUMENT_ERROR",
ErrorKind::YKPIV_RANGE_ERROR => "YKPIV_RANGE_ERROR",
ErrorKind::YKPIV_NOT_SUPPORTED => "YKPIV_NOT_SUPPORTED",
ErrorKind::Ok => "YKPIV_OK",
ErrorKind::MemoryError => "YKPIV_MEMORY_ERROR",
ErrorKind::PcscError => "YKPIV_PCSC_ERROR",
ErrorKind::SizeError => "YKPIV_SIZE_ERROR",
ErrorKind::AppletError => "YKPIV_APPLET_ERROR",
ErrorKind::AuthenticationError => "YKPIV_AUTHENTICATION_ERROR",
ErrorKind::RandomnessError => "YKPIV_RANDOMNESS_ERROR",
ErrorKind::GenericError => "YKPIV_GENERIC_ERROR",
ErrorKind::KeyError => "YKPIV_KEY_ERROR",
ErrorKind::ParseError => "YKPIV_PARSE_ERROR",
ErrorKind::WrongPin => "YKPIV_WRONG_PIN",
ErrorKind::InvalidObject => "YKPIV_INVALID_OBJECT",
ErrorKind::AlgorithmError => "YKPIV_ALGORITHM_ERROR",
ErrorKind::PinLocked => "YKPIV_PIN_LOCKED",
ErrorKind::ArgumentError => "YKPIV_ARGUMENT_ERROR",
ErrorKind::RangeError => "YKPIV_RANGE_ERROR",
ErrorKind::NotSupported => "YKPIV_NOT_SUPPORTED",
}
}
/// Error message
pub fn msg(self) -> &'static str {
match self {
ErrorKind::YKPIV_OK => "OK",
ErrorKind::YKPIV_MEMORY_ERROR => "memory error",
ErrorKind::YKPIV_PCSC_ERROR => "PCSC error",
ErrorKind::YKPIV_SIZE_ERROR => "size error",
ErrorKind::YKPIV_APPLET_ERROR => "applet error",
ErrorKind::YKPIV_AUTHENTICATION_ERROR => "authentication error",
ErrorKind::YKPIV_RANDOMNESS_ERROR => "randomness error",
ErrorKind::YKPIV_GENERIC_ERROR => "generic error",
ErrorKind::YKPIV_KEY_ERROR => "key error",
ErrorKind::YKPIV_PARSE_ERROR => "parse error",
ErrorKind::YKPIV_WRONG_PIN => "wrong pin",
ErrorKind::YKPIV_INVALID_OBJECT => "invalid object",
ErrorKind::YKPIV_ALGORITHM_ERROR => "algorithm error",
ErrorKind::YKPIV_PIN_LOCKED => "PIN locked",
ErrorKind::YKPIV_ARGUMENT_ERROR => "argument error",
ErrorKind::YKPIV_RANGE_ERROR => "range error",
ErrorKind::YKPIV_NOT_SUPPORTED => "not supported",
ErrorKind::Ok => "OK",
ErrorKind::MemoryError => "memory error",
ErrorKind::PcscError => "PCSC error",
ErrorKind::SizeError => "size error",
ErrorKind::AppletError => "applet error",
ErrorKind::AuthenticationError => "authentication error",
ErrorKind::RandomnessError => "randomness error",
ErrorKind::GenericError => "generic error",
ErrorKind::KeyError => "key error",
ErrorKind::ParseError => "parse error",
ErrorKind::WrongPin => "wrong pin",
ErrorKind::InvalidObject => "invalid object",
ErrorKind::AlgorithmError => "algorithm error",
ErrorKind::PinLocked => "PIN locked",
ErrorKind::ArgumentError => "argument error",
ErrorKind::RangeError => "range error",
ErrorKind::NotSupported => "not supported",
}
}
}
@@ -109,24 +147,14 @@ impl fmt::Display for ErrorKind {
impl std::error::Error for ErrorKind {}
#[derive(Copy)]
#[repr(C)]
pub struct Error {
pub rc: ErrorKind,
pub name: *const u8,
pub description: *const u8,
}
impl Clone for Error {
fn clone(&self) -> Self {
*self
}
}
/// Get a string representation of this error
// TODO(tarcieri): completely replace this with `Display`
pub fn ykpiv_strerror(err: ErrorKind) -> &'static str {
err.msg()
}
/// Get the name of this error
// TODO(tarcieri): completely replace this with debug
pub fn ykpiv_strerror_name(err: ErrorKind) -> &'static str {
err.name()
}