Rewrite translated code to use the pcsc crate

This commit contains a "big bang" refactor/rewrite which does the
following:

- Replaces all `SCard*` FFI calls with the `pcsc` crate, which provides
  a safe, portable PC/SC API across Windows, macOS, and Linux
- Refactors the `util` module into modules representing the various
  device functions and concepts, e.g. `certificate`, `key`, `mgm`
- Replaces all usage of `libc` with `std` functionality, and in many
  places rewriting functionality to use safe code.
- Removes `ykpiv_` from all function names, and `Piv*` from type names.

In 20/20 hindsight I wish I had done this commit more incrementally so
as to make it easier to review. Que sera sera.

However, realistically we need to test all functionality on the device
to ensure that it actually works. Going forward I would like to put
pretty much all of the current code behind an `untested` cargo feature,
and then remove it for each bit of functionality we test.
This commit is contained in:
Tony Arcieri
2019-11-24 15:57:39 -08:00
parent 96cd5d080b
commit ebbf043bc9
23 changed files with 3487 additions and 4123 deletions
+23 -16
View File
@@ -30,7 +30,7 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use std::fmt;
use std::fmt::{self, Display};
/// Kinds of errors
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -39,7 +39,10 @@ pub enum Error {
MemoryError,
/// PCSC error
PcscError,
PcscError {
/// Original PC/SC error
inner: Option<pcsc::Error>,
},
/// Size error
SizeError,
@@ -65,7 +68,7 @@ pub enum Error {
/// Wrong PIN
WrongPin {
/// Number of tries remaining
tries: i32,
tries: u32,
},
/// Invalid object
@@ -95,7 +98,7 @@ impl Error {
pub fn name(self) -> &'static str {
match self {
Error::MemoryError => "YKPIV_MEMORY_ERROR",
Error::PcscError => "YKPIV_PCSC_ERROR",
Error::PcscError { .. } => "YKPIV_PCSC_ERROR",
Error::SizeError => "YKPIV_SIZE_ERROR",
Error::AppletError => "YKPIV_APPLET_ERROR",
Error::AuthenticationError => "YKPIV_AUTHENTICATION_ERROR",
@@ -117,7 +120,7 @@ impl Error {
pub fn msg(self) -> &'static str {
match self {
Error::MemoryError => "memory error",
Error::PcscError => "PCSC error",
Error::PcscError { .. } => "PCSC error",
Error::SizeError => "size error",
Error::AppletError => "applet error",
Error::AuthenticationError => "authentication error",
@@ -136,22 +139,26 @@ impl Error {
}
}
impl fmt::Display for Error {
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.msg())
}
}
impl std::error::Error for Error {}
/// Get a string representation of this error
// TODO(tarcieri): completely replace this with `Display`
pub fn ykpiv_strerror(err: Error) -> &'static str {
err.msg()
impl From<pcsc::Error> for Error {
fn from(err: pcsc::Error) -> Error {
Error::PcscError { inner: Some(err) }
}
}
/// Get the name of this error
// TODO(tarcieri): completely replace this with debug
pub fn ykpiv_strerror_name(err: Error) -> &'static str {
err.name()
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
#[allow(trivial_casts)] // why doesn't this work without the cast???
Error::PcscError { inner } => inner
.as_ref()
.map(|err| err as &(dyn std::error::Error + 'static)),
_ => None,
}
}
}