Skip to content

Commit 8fb8cf8

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

File tree

2 files changed

+67
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)