Skip to content

Commit 8eb5ff8

Browse files
committed
Add support to get session validation flags
Signed-off-by: Jakub Jelen <[email protected]>
1 parent ed8cfbe commit 8eb5ff8

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

cryptoki/src/session/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ mod session_info;
2121
mod session_management;
2222
mod signing_macing;
2323
mod slot_token_management;
24+
mod validation;
2425

2526
pub use object_management::ObjectHandleIterator;
2627
pub use session_info::{SessionInfo, SessionState};
28+
pub use validation::ValidationFlagsType;
2729

2830
/// Type that identifies a session
2931
///

cryptoki/src/session/validation.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2025 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//! Session Validation
4+
5+
use crate::context::Function;
6+
use crate::error::{Result, Rv};
7+
use crate::session::Session;
8+
use cryptoki_sys::*;
9+
use std::fmt::{Debug, Formatter};
10+
use std::ops::Deref;
11+
12+
/// The type of validation flag to query
13+
#[derive(Copy, Clone, Debug)]
14+
pub struct ValidationFlagsType {
15+
val: CK_SESSION_VALIDATION_FLAGS_TYPE,
16+
}
17+
18+
impl ValidationFlagsType {
19+
/// Check the last operation met all requirements of a validated mechanism.
20+
pub const VALIDATION_OK: ValidationFlagsType = ValidationFlagsType {
21+
val: CKS_LAST_VALIDATION_OK,
22+
};
23+
24+
pub(crate) fn stringify(flags: CK_SESSION_VALIDATION_FLAGS_TYPE) -> String {
25+
match flags {
26+
CKS_LAST_VALIDATION_OK => String::from(stringify!(CKS_LAST_VALIDATION_OK)),
27+
_ => format!("unknown ({flags:08x})"),
28+
}
29+
}
30+
}
31+
32+
impl std::fmt::Display for ValidationFlagsType {
33+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34+
write!(f, "{}", ValidationFlagsType::stringify(self.val))
35+
}
36+
}
37+
38+
impl Deref for ValidationFlagsType {
39+
type Target = CK_SESSION_VALIDATION_FLAGS_TYPE;
40+
41+
fn deref(&self) -> &Self::Target {
42+
&self.val
43+
}
44+
}
45+
46+
impl From<ValidationFlagsType> for CK_SESSION_VALIDATION_FLAGS_TYPE {
47+
fn from(val: ValidationFlagsType) -> Self {
48+
*val
49+
}
50+
}
51+
52+
impl Session {
53+
/// Get requested validation flags from the session
54+
///
55+
/// The only supported flag as for PKCS#11 3.2 is `ValidationFlagsType::VALIDATION_OK`
56+
pub fn get_validation_flags(&self, flags_type: ValidationFlagsType) -> Result<CK_FLAGS> {
57+
let mut flags: CK_FLAGS = 0;
58+
unsafe {
59+
Rv::from(get_pkcs11!(self.client(), C_GetSessionValidationFlags)(
60+
self.handle(),
61+
flags_type.into(),
62+
&mut flags,
63+
))
64+
.into_result(Function::GetSessionValidationFlags)?;
65+
}
66+
Ok(flags)
67+
}
68+
}

0 commit comments

Comments
 (0)