Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session_management: Add ability to login with raw bytes #90

Merged
merged 1 commit into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cryptoki/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ impl Session {
session_management::login(self, user_type, pin)
}

/// Logs a session in using a slice of raw bytes as a PIN. Some dongle drivers allow
/// non UTF-8 characters in the PIN and as a result, we aren't guaranteed that we can
/// pass in a UTF-8 string to login. Therefore, it's useful to be able to pass in raw bytes
/// rather than convert a UTF-8 string to bytes.
///
/// # Arguments
///
/// * `user_type` - The type of user to log in as
/// * `pin` - The PIN to use
///
/// _NOTE: By passing `None` into `login`, you must ensure that the
/// [CKF_PROTECTED_AUTHENTICATION_PATH] flag is set in the `TokenFlags`._
pub fn login_with_raw(&self, user_type: UserType, pin: &[u8]) -> Result<()> {
session_management::login_with_raw(self, user_type, pin)
}

/// Log a session out
pub fn logout(&self) -> Result<()> {
session_management::logout(self)
Expand Down
14 changes: 14 additions & 0 deletions cryptoki/src/session/session_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ pub(super) fn login(session: &Session, user_type: UserType, pin: Option<&str>) -
}
}

// See public docs on stub in parent mod.rs
#[inline(always)]
pub(super) fn login_with_raw(session: &Session, user_type: UserType, pin: &[u8]) -> Result<()> {
unsafe {
Rv::from(get_pkcs11!(session.client(), C_Login)(
session.handle(),
user_type.into(),
pin.as_ptr() as *mut u8,
pin.len().try_into()?,
))
.into_result()
}
}

// See public docs on stub in parent mod.rs
#[inline(always)]
pub(super) fn logout(session: &Session) -> Result<()> {
Expand Down