From 9ff89bab8057f525d6c28b47c92bbe5a28de2e47 Mon Sep 17 00:00:00 2001 From: cj Date: Mon, 6 May 2024 21:13:53 +0800 Subject: [PATCH] Save the name when it is opened --- skf-rs/src/engine/app.rs | 26 +++++++++++++++++++------- skf-rs/src/engine/device.rs | 22 ++++++++++++++-------- skf-rs/src/engine/manager.rs | 6 +++--- skf-rs/src/lib.rs | 11 ++++++++++- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/skf-rs/src/engine/app.rs b/skf-rs/src/engine/app.rs index 90b3630..44518fb 100644 --- a/skf-rs/src/engine/app.rs +++ b/skf-rs/src/engine/app.rs @@ -19,6 +19,7 @@ pub(crate) struct SkfAppImpl { lib: Arc, symbols: ModApp, handle: HANDLE, + name: String, } impl SkfAppImpl { @@ -27,13 +28,14 @@ impl SkfAppImpl { /// [handle] - The application handle /// /// [lib] - The library handle - pub fn new(handle: HANDLE, lib: &Arc) -> crate::Result { + pub fn new(handle: HANDLE, name: &str, lib: &Arc) -> crate::Result { let lc = Arc::clone(lib); let symbols = ModApp::load_symbols(lib)?; Ok(Self { lib: lc, symbols, handle, + name: name.to_string(), }) } @@ -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 { @@ -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))), } } @@ -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))), } } @@ -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 { @@ -469,6 +475,7 @@ pub(crate) struct SkfContainerImpl { lib: Arc, symbols: ModContainer, handle: HANDLE, + name: String, } impl SkfContainerImpl { @@ -477,13 +484,14 @@ impl SkfContainerImpl { /// [handle] - The application handle /// /// [lib] - The library handle - pub fn new(handle: HANDLE, lib: &Arc) -> crate::Result { + pub fn new(handle: HANDLE, name: &str, lib: &Arc) -> crate::Result { let lc = Arc::clone(lib); let symbols = ModContainer::load_symbols(lib)?; Ok(Self { symbols, handle, lib: lc, + name: name.to_string(), }) } @@ -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 { @@ -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 { let func = self.symbols.ct_get_type.as_ref().expect("Symbol not load"); diff --git a/skf-rs/src/engine/device.rs b/skf-rs/src/engine/device.rs index 10d63e2..ea08b05 100644 --- a/skf-rs/src/engine/device.rs +++ b/skf-rs/src/engine/device.rs @@ -23,6 +23,7 @@ pub(crate) struct SkfDeviceImpl { lib: Arc, symbols: ModDev, handle: HANDLE, + name: String, } impl SkfDeviceImpl { @@ -31,13 +32,14 @@ impl SkfDeviceImpl { /// [handle] - Native handle /// /// [lib] - The library handle - pub fn new(handle: HANDLE, lib: &Arc) -> Result { + pub fn new(handle: HANDLE, name: &str, lib: &Arc) -> Result { let lc = Arc::clone(lib); let symbols = ModDev::load_symbols(lib)?; Ok(Self { lib: lc, symbols, handle, + name: name.to_string(), }) } @@ -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) } } @@ -423,14 +425,14 @@ impl AppManager for SkfDeviceImpl { #[instrument] fn create_app(&self, name: &str, attr: &AppAttr) -> Result> { 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, @@ -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> { 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)) } @@ -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) { diff --git a/skf-rs/src/engine/manager.rs b/skf-rs/src/engine/manager.rs index 659baba..7401968 100644 --- a/skf-rs/src/engine/manager.rs +++ b/skf-rs/src/engine/manager.rs @@ -126,14 +126,14 @@ impl DeviceManager for ManagerImpl { #[instrument] fn connect(&self, device_name: &str) -> Result> { 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)) } diff --git a/skf-rs/src/lib.rs b/skf-rs/src/lib.rs index 4514169..499ed18 100644 --- a/skf-rs/src/lib.rs +++ b/skf-rs/src/lib.rs @@ -322,6 +322,9 @@ pub trait DeviceAuth { pub trait SkfDevice: DeviceCtl + DeviceAuth + AppManager + DeviceCrypto { /// Block cipher service fn block_cipher(&self) -> Result>; + + /// The name when it is opened + fn name(&self) -> &str; } /// PIN type: Admin @@ -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; @@ -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]