use cryptoki_sys::{CKF_RW_SESSION, CKF_SERIAL_SESSION};
use crate::context::Pkcs11;
use crate::error::{Result, Rv};
use crate::session::{CloseOnDrop, Session};
use crate::slot::Slot;
use super::Function;
impl Pkcs11 {
#[inline(always)]
fn open_session(
&self,
slot_id: Slot,
read_write: bool,
close_on_drop: CloseOnDrop,
) -> Result<Session> {
let mut session_handle = 0;
let flags = if read_write {
CKF_SERIAL_SESSION | CKF_RW_SESSION
} else {
CKF_SERIAL_SESSION
};
unsafe {
Rv::from(get_pkcs11!(self, C_OpenSession)(
slot_id.into(),
flags,
std::ptr::null_mut(),
None,
&mut session_handle,
))
.into_result(Function::OpenSession)?;
}
Ok(Session::new(session_handle, self.clone(), close_on_drop))
}
pub fn open_ro_session(&self, slot_id: Slot) -> Result<Session> {
self.open_session(slot_id, false, CloseOnDrop::AutomaticallyCloseSession)
}
pub fn open_rw_session(&self, slot_id: Slot) -> Result<Session> {
self.open_session(slot_id, true, CloseOnDrop::AutomaticallyCloseSession)
}
pub fn open_ro_session_no_drop(&self, slot_id: Slot) -> Result<Session> {
self.open_session(slot_id, false, CloseOnDrop::DoNotClose)
}
pub fn open_rw_session_no_drop(&self, slot_id: Slot) -> Result<Session> {
self.open_session(slot_id, true, CloseOnDrop::DoNotClose)
}
}