Skip to content

Commit 83726ed

Browse files
authored
Merge pull request #90 from Subject38/login_raw
session_management: Add ability to login with raw bytes
2 parents 20e8eae + 95f317e commit 83726ed

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

cryptoki/src/session/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ impl Session {
100100
session_management::login(self, user_type, pin)
101101
}
102102

103+
/// Logs a session in using a slice of raw bytes as a PIN. Some dongle drivers allow
104+
/// non UTF-8 characters in the PIN and as a result, we aren't guaranteed that we can
105+
/// pass in a UTF-8 string to login. Therefore, it's useful to be able to pass in raw bytes
106+
/// rather than convert a UTF-8 string to bytes.
107+
///
108+
/// # Arguments
109+
///
110+
/// * `user_type` - The type of user to log in as
111+
/// * `pin` - The PIN to use
112+
///
113+
/// _NOTE: By passing `None` into `login`, you must ensure that the
114+
/// [CKF_PROTECTED_AUTHENTICATION_PATH] flag is set in the `TokenFlags`._
115+
pub fn login_with_raw(&self, user_type: UserType, pin: &[u8]) -> Result<()> {
116+
session_management::login_with_raw(self, user_type, pin)
117+
}
118+
103119
/// Log a session out
104120
pub fn logout(&self) -> Result<()> {
105121
session_management::logout(self)

cryptoki/src/session/session_management.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ pub(super) fn login(session: &Session, user_type: UserType, pin: Option<&str>) -
3636
}
3737
}
3838

39+
// See public docs on stub in parent mod.rs
40+
#[inline(always)]
41+
pub(super) fn login_with_raw(session: &Session, user_type: UserType, pin: &[u8]) -> Result<()> {
42+
unsafe {
43+
Rv::from(get_pkcs11!(session.client(), C_Login)(
44+
session.handle(),
45+
user_type.into(),
46+
pin.as_ptr() as *mut u8,
47+
pin.len().try_into()?,
48+
))
49+
.into_result()
50+
}
51+
}
52+
3953
// See public docs on stub in parent mod.rs
4054
#[inline(always)]
4155
pub(super) fn logout(session: &Session) -> Result<()> {

0 commit comments

Comments
 (0)