Skip to content

Commit ded651c

Browse files
vkkoskieionut-arm
authored andcommitted
Refactor SessionInfo
Signed-off-by: Keith Koskie <[email protected]>
1 parent 705bee7 commit ded651c

File tree

4 files changed

+80
-43
lines changed

4 files changed

+80
-43
lines changed

cryptoki/src/session/mod.rs

+3-37
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ use crate::context::Pkcs11;
66
use crate::error::Result;
77
use crate::mechanism::Mechanism;
88
use crate::object::{Attribute, AttributeInfo, AttributeType, ObjectHandle};
9-
use crate::slot::Slot;
10-
use crate::types::Ulong;
119

1210
use cryptoki_sys::*;
1311
use log::error;
1412
use std::collections::HashMap;
15-
use std::convert::TryInto;
1613
use std::fmt::Formatter;
1714
use std::marker::PhantomData;
1815
use std::ops::Deref;
@@ -23,10 +20,13 @@ mod flags;
2320
mod key_management;
2421
mod object_management;
2522
mod random;
23+
mod session_info;
2624
mod session_management;
2725
mod signing_macing;
2826
mod slot_token_management;
2927

28+
pub use session_info::SessionInfo;
29+
3030
pub use flags::*;
3131

3232
/// Type that identifies a session
@@ -443,37 +443,3 @@ impl std::fmt::Display for SessionState {
443443
write!(f, "{}", SessionState::stringify(self.val))
444444
}
445445
}
446-
447-
/// Type identifying the session information
448-
#[derive(Copy, Clone, Debug)]
449-
pub struct SessionInfo {
450-
val: CK_SESSION_INFO,
451-
}
452-
453-
impl SessionInfo {
454-
pub(crate) fn new(val: CK_SESSION_INFO) -> Self {
455-
Self { val }
456-
}
457-
458-
/// Returns an error code defined by the cryptographic device
459-
pub fn device_error(&self) -> Ulong {
460-
self.val.ulDeviceError.into()
461-
}
462-
463-
/// Returns the flags for this session
464-
pub fn flags(&self) -> SessionFlags {
465-
self.val.flags.into()
466-
}
467-
468-
/// Returns the state of the session
469-
pub fn session_state(&self) -> SessionState {
470-
self.val.state.into()
471-
}
472-
473-
/// Returns the slot the session is on
474-
pub fn slot_id(&self) -> Slot {
475-
// The unwrap should not fail as `slotID` is a `CK_SLOT_ID ` which is the same type as
476-
// `slot_id` within the `Slot` structure
477-
self.val.slotID.try_into().unwrap()
478-
}
479-
}

cryptoki/src/session/session_info.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2021 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//! Session info
4+
5+
use crate::{
6+
flag::{CkFlags, FlagBit},
7+
slot::Slot,
8+
};
9+
use cryptoki_sys::*;
10+
use std::fmt::{self, Debug, Formatter};
11+
12+
use super::SessionState;
13+
14+
const RW_SESSION: FlagBit<SessionInfo> = FlagBit::new(CKF_RW_SESSION);
15+
const SERIAL_SESSION: FlagBit<SessionInfo> = FlagBit::new(CKF_SERIAL_SESSION);
16+
17+
impl Debug for CkFlags<SessionInfo> {
18+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
19+
f.debug_struct("Flags")
20+
.field("rw_session", &(self.contains(RW_SESSION)))
21+
.field("serial_session", &(self.contains(SERIAL_SESSION)))
22+
.finish()
23+
}
24+
}
25+
26+
/// Provides information about a session
27+
#[derive(Copy, Clone, Debug)]
28+
pub struct SessionInfo {
29+
slot_id: Slot,
30+
state: SessionState,
31+
flags: CkFlags<Self>,
32+
device_error: u64,
33+
}
34+
35+
impl SessionInfo {
36+
/// ID of the slot that interfaces the token
37+
pub fn slot_id(&self) -> Slot {
38+
self.slot_id
39+
}
40+
41+
/// The state of the session
42+
pub fn session_state(&self) -> SessionState {
43+
self.state
44+
}
45+
46+
/// True if the session has R/W access to token objects, and false if access
47+
/// is read-only
48+
pub fn read_write(&self) -> bool {
49+
self.flags.contains(RW_SESSION)
50+
}
51+
52+
/// An error code defined by the cryptographic device (used for errors not
53+
/// covered by PKCS#11)
54+
pub fn device_error(&self) -> u64 {
55+
self.device_error
56+
}
57+
}
58+
59+
#[doc(hidden)]
60+
impl From<CK_SESSION_INFO> for SessionInfo {
61+
fn from(val: CK_SESSION_INFO) -> Self {
62+
#[allow(trivial_numeric_casts)]
63+
let device_error = val.ulDeviceError as u64;
64+
Self {
65+
slot_id: Slot::new(val.slotID),
66+
state: val.state.into(),
67+
flags: CkFlags::from(val.flags),
68+
device_error,
69+
}
70+
}
71+
}

cryptoki/src/session/session_management.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ pub(super) fn get_session_info(session: &Session) -> Result<SessionInfo> {
5252
&mut session_info,
5353
))
5454
.into_result()?;
55-
Ok(SessionInfo::new(session_info))
55+
Ok(SessionInfo::from(session_info))
5656
}
5757
}

cryptoki/tests/basic.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ fn get_session_info_test() -> Result<()> {
477477
{
478478
let session = pkcs11.open_session_no_callback(slot, flags)?;
479479
let session_info = session.get_session_info()?;
480-
assert_eq!(session_info.flags(), flags);
480+
assert!(!session_info.read_write());
481481
assert_eq!(session_info.slot_id(), slot);
482482
assert_eq!(
483483
session_info.session_state(),
@@ -486,7 +486,7 @@ fn get_session_info_test() -> Result<()> {
486486

487487
session.login(UserType::User, Some(USER_PIN))?;
488488
let session_info = session.get_session_info()?;
489-
assert_eq!(session_info.flags(), flags);
489+
assert!(!session_info.read_write());
490490
assert_eq!(session_info.slot_id(), slot);
491491
assert_eq!(
492492
session_info.session_state(),
@@ -506,7 +506,7 @@ fn get_session_info_test() -> Result<()> {
506506

507507
let session = pkcs11.open_session_no_callback(slot, flags)?;
508508
let session_info = session.get_session_info()?;
509-
assert_eq!(session_info.flags(), flags);
509+
assert!(session_info.read_write());
510510
assert_eq!(session_info.slot_id(), slot);
511511
assert_eq!(
512512
session_info.session_state(),
@@ -515,7 +515,7 @@ fn get_session_info_test() -> Result<()> {
515515

516516
session.login(UserType::User, Some(USER_PIN))?;
517517
let session_info = session.get_session_info()?;
518-
assert_eq!(session_info.flags(), flags);
518+
assert!(session_info.read_write());
519519
assert_eq!(session_info.slot_id(), slot);
520520
assert_eq!(
521521
session_info.session_state(),
@@ -524,7 +524,7 @@ fn get_session_info_test() -> Result<()> {
524524
session.logout()?;
525525
session.login(UserType::So, Some(SO_PIN))?;
526526
let session_info = session.get_session_info()?;
527-
assert_eq!(session_info.flags(), flags);
527+
assert!(session_info.read_write());
528528
assert_eq!(session_info.slot_id(), slot);
529529
assert_eq!(session_info.session_state(), SessionState::RW_SO_FUNCTIONS);
530530

0 commit comments

Comments
 (0)