Skip to content

Commit

Permalink
Merge pull request #8 from power4j/feature/fe
Browse files Browse the repository at this point in the history
Save the name when it is opened
  • Loading branch information
John-Chan authored May 6, 2024
2 parents cb3d3a4 + b046f40 commit 8d3c7d3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
26 changes: 19 additions & 7 deletions skf-rs/src/engine/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(crate) struct SkfAppImpl {
lib: Arc<libloading::Library>,
symbols: ModApp,
handle: HANDLE,
name: String,
}

impl SkfAppImpl {
Expand All @@ -27,13 +28,14 @@ impl SkfAppImpl {
/// [handle] - The application handle
///
/// [lib] - The library handle
pub fn new(handle: HANDLE, lib: &Arc<libloading::Library>) -> crate::Result<Self> {
pub fn new(handle: HANDLE, name: &str, lib: &Arc<libloading::Library>) -> crate::Result<Self> {
let lc = Arc::clone(lib);
let symbols = ModApp::load_symbols(lib)?;
Ok(Self {
lib: lc,
symbols,
handle,
name: name.to_string(),
})
}

Expand All @@ -52,7 +54,7 @@ impl SkfAppImpl {

impl Debug for SkfAppImpl {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "SkfAppImpl")
write!(f, "SkfAppImpl({})", &self.name)
}
}
impl Drop for SkfAppImpl {
Expand Down Expand Up @@ -366,7 +368,7 @@ impl ContainerManager for SkfAppImpl {
let ret = unsafe { func(self.handle, container_name.as_ptr() as LPSTR, &mut handle) };
trace!("[SKF_CreateContainer]: ret = {}", ret);
match ret {
SAR_OK => Ok(Box::new(SkfContainerImpl::new(handle, &self.lib)?)),
SAR_OK => Ok(Box::new(SkfContainerImpl::new(handle, name, &self.lib)?)),
_ => Err(Error::Skf(SkfErr::of_code(ret))),
}
}
Expand All @@ -384,7 +386,7 @@ impl ContainerManager for SkfAppImpl {
let ret = unsafe { func(self.handle, container_name.as_ptr() as LPSTR, &mut handle) };
trace!("[SKF_OpenContainer]: ret = {}", ret);
match ret {
SAR_OK => Ok(Box::new(SkfContainerImpl::new(handle, &self.lib)?)),
SAR_OK => Ok(Box::new(SkfContainerImpl::new(handle, name, &self.lib)?)),
_ => Err(Error::Skf(SkfErr::of_code(ret))),
}
}
Expand All @@ -407,7 +409,11 @@ impl ContainerManager for SkfAppImpl {
}
}

impl SkfApp for SkfAppImpl {}
impl SkfApp for SkfAppImpl {
fn name(&self) -> &str {
&self.name
}
}
impl From<&FileAttribute> for FileAttr {
fn from(value: &FileAttribute) -> Self {
let file_name: String = unsafe {
Expand Down Expand Up @@ -469,6 +475,7 @@ pub(crate) struct SkfContainerImpl {
lib: Arc<libloading::Library>,
symbols: ModContainer,
handle: HANDLE,
name: String,
}

impl SkfContainerImpl {
Expand All @@ -477,13 +484,14 @@ impl SkfContainerImpl {
/// [handle] - The application handle
///
/// [lib] - The library handle
pub fn new(handle: HANDLE, lib: &Arc<libloading::Library>) -> crate::Result<Self> {
pub fn new(handle: HANDLE, name: &str, lib: &Arc<libloading::Library>) -> crate::Result<Self> {
let lc = Arc::clone(lib);
let symbols = ModContainer::load_symbols(lib)?;
Ok(Self {
symbols,
handle,
lib: lc,
name: name.to_string(),
})
}

Expand All @@ -502,7 +510,7 @@ impl SkfContainerImpl {

impl Debug for SkfContainerImpl {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "SkfContainerImpl")
write!(f, "SkfContainerImpl({})", &self.name)
}
}
impl Drop for SkfContainerImpl {
Expand All @@ -511,6 +519,10 @@ impl Drop for SkfContainerImpl {
}
}
impl SkfContainer for SkfContainerImpl {
fn name(&self) -> &str {
&self.name
}

#[instrument]
fn get_type(&self) -> crate::Result<u32> {
let func = self.symbols.ct_get_type.as_ref().expect("Symbol not load");
Expand Down
22 changes: 14 additions & 8 deletions skf-rs/src/engine/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub(crate) struct SkfDeviceImpl {
lib: Arc<libloading::Library>,
symbols: ModDev,
handle: HANDLE,
name: String,
}

impl SkfDeviceImpl {
Expand All @@ -31,13 +32,14 @@ impl SkfDeviceImpl {
/// [handle] - Native handle
///
/// [lib] - The library handle
pub fn new(handle: HANDLE, lib: &Arc<libloading::Library>) -> Result<Self> {
pub fn new(handle: HANDLE, name: &str, lib: &Arc<libloading::Library>) -> Result<Self> {
let lc = Arc::clone(lib);
let symbols = ModDev::load_symbols(lib)?;
Ok(Self {
lib: lc,
symbols,
handle,
name: name.to_string(),
})
}

Expand All @@ -57,7 +59,7 @@ impl SkfDeviceImpl {

impl Debug for SkfDeviceImpl {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "SkfDeviceImpl")
write!(f, "SkfDeviceImpl({})", &self.name)
}
}

Expand Down Expand Up @@ -423,14 +425,14 @@ impl AppManager for SkfDeviceImpl {
#[instrument]
fn create_app(&self, name: &str, attr: &AppAttr) -> Result<Box<dyn SkfApp>> {
let func = self.symbols.app_create.as_ref().expect("Symbol not load");
let name = param::as_cstring("name", name)?;
let c_name = param::as_cstring("name", name)?;
let admin_pin = param::as_cstring("AppAttr.admin_pin", &attr.admin_pin)?;
let user_pin = param::as_cstring("AppAttr.user_pin", &attr.user_pin)?;
let mut handle: HANDLE = std::ptr::null_mut();
let ret = unsafe {
func(
self.handle,
name.as_ptr() as *const CHAR,
c_name.as_ptr() as *const CHAR,
admin_pin.as_ptr() as *const CHAR,
attr.admin_pin_retry_count as DWORD,
user_pin.as_ptr() as *const CHAR,
Expand All @@ -443,21 +445,21 @@ impl AppManager for SkfDeviceImpl {
if ret != SAR_OK {
return Err(Error::Skf(SkfErr::of_code(ret)));
}
let app = SkfAppImpl::new(handle, &self.lib)?;
let app = SkfAppImpl::new(handle, name, &self.lib)?;
Ok(Box::new(app))
}

#[instrument]
fn open_app(&self, name: &str) -> Result<Box<dyn SkfApp>> {
let func = self.symbols.app_open.as_ref().expect("Symbol not load");
let name = param::as_cstring("name", name)?;
let c_name = param::as_cstring("name", name)?;
let mut handle: HANDLE = std::ptr::null_mut();
let ret = unsafe { func(self.handle, name.as_ptr() as LPSTR, &mut handle) };
let ret = unsafe { func(self.handle, c_name.as_ptr() as LPSTR, &mut handle) };
trace!("[SKF_OpenApplication]: ret = {}", ret);
if ret != SAR_OK {
return Err(Error::Skf(SkfErr::of_code(ret)));
}
let app = SkfAppImpl::new(handle, &self.lib)?;
let app = SkfAppImpl::new(handle, name, &self.lib)?;
Ok(Box::new(app))
}

Expand All @@ -479,6 +481,10 @@ impl SkfDevice for SkfDeviceImpl {
let crypto = crypto::SkfBlockCipherImpl::new(&self.lib)?;
Ok(Box::new(crypto))
}

fn name(&self) -> &str {
&self.name
}
}
impl Drop for SkfDeviceImpl {
fn drop(&mut self) {
Expand Down
6 changes: 3 additions & 3 deletions skf-rs/src/engine/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ impl DeviceManager for ManagerImpl {
#[instrument]
fn connect(&self, device_name: &str) -> Result<Box<dyn SkfDevice>> {
let func = self.symbols.connect_dev.as_ref().expect("Symbol not load");
let device_name = param::as_cstring("device_name", device_name)?;
let c_name = param::as_cstring("device_name", device_name)?;
let mut handle: HANDLE = std::ptr::null_mut();
let ret = unsafe { func(device_name.as_ptr() as *const CHAR, &mut handle) };
let ret = unsafe { func(c_name.as_ptr() as *const CHAR, &mut handle) };
trace!("[SKF_ConnectDev]: ret = {}", ret);
if ret != SAR_OK {
return Err(Error::Skf(SkfErr::of_code(ret)));
}
let dev = SkfDeviceImpl::new(handle, &self.lib)?;
let dev = SkfDeviceImpl::new(handle, device_name, &self.lib)?;
Ok(Box::new(dev))
}

Expand Down
11 changes: 10 additions & 1 deletion skf-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ pub trait DeviceAuth {
pub trait SkfDevice: DeviceCtl + DeviceAuth + AppManager + DeviceCrypto {
/// Block cipher service
fn block_cipher(&self) -> Result<Box<dyn SkfBlockCipher + Send + Sync>>;

/// The name when it is opened
fn name(&self) -> &str;
}

/// PIN type: Admin
Expand Down Expand Up @@ -481,7 +484,10 @@ pub trait ContainerManager {
/// Represents an Application instance
/// ## Close
/// Application instance is closed when `Drop`
pub trait SkfApp: AppSecurity + FileManager + ContainerManager {}
pub trait SkfApp: AppSecurity + FileManager + ContainerManager {
/// The name when it is opened
fn name(&self) -> &str;
}

const CONTAINER_TYPE_UNKNOWN: u32 = 0;
const CONTAINER_TYPE_RSA: u32 = 0;
Expand All @@ -493,6 +499,9 @@ const CONTAINER_TYPE_ECC: u32 = 0;
/// ## Owner object lifetime requirement
/// If owner object([SkfApp]) is dropped, the `SkfContainer` object will be invalid
pub trait SkfContainer {
/// The name when it is opened
fn name(&self) -> &str;

/// Get container type,the value of type can be:
/// - [CONTAINER_TYPE_UNKNOWN]
/// - [CONTAINER_TYPE_RSA]
Expand Down

0 comments on commit 8d3c7d3

Please sign in to comment.