1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#[macro_use]
mod error;
#[cfg(feature = "http")]
pub mod http;
#[cfg(feature = "usb")]
pub mod usb;

use std::fmt::Debug;
use uuid::Uuid;

pub use self::error::{AdapterError, AdapterErrorKind};
use serial_number::SerialNumber;

/// Adapters for communicating with the YubiHSM2
pub trait Adapter: Sized + Send + Sync {
    /// Configuration options for this adapter
    type Config: Debug + Default + Send + Sync;

    /// Open a connection to this adapter
    fn open(config: &Self::Config) -> Result<Self, AdapterError>;

    /// Are we able to send/receive messages to/from the HSM?
    fn healthcheck(&self) -> Result<(), AdapterError>;

    /// Get the serial number for the current YubiHSM2 (if available)
    fn serial_number(&self) -> Result<SerialNumber, AdapterError>;

    /// Send a command message to the HSM, then read and return the response
    fn send_message(&self, uuid: Uuid, msg: Vec<u8>) -> Result<Vec<u8>, AdapterError>;
}