Skip to content

Commit 77c9433

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

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 => {
27+
String::from(stringify!(CKS_LAST_VALIDATION_OK))
28+
},
29+
_ => format!("unknown ({flags:08x})"),
30+
}
31+
}
32+
}
33+
34+
impl std::fmt::Display for ValidationFlagsType {
35+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36+
write!(f, "{}", ValidationFlagsType::stringify(self.val))
37+
}
38+
}
39+
40+
41+
impl Deref for ValidationFlagsType {
42+
type Target = CK_SESSION_VALIDATION_FLAGS_TYPE;
43+
44+
fn deref(&self) -> &Self::Target {
45+
&self.val
46+
}
47+
}
48+
49+
impl From<ValidationFlagsType> for CK_SESSION_VALIDATION_FLAGS_TYPE {
50+
fn from(val: ValidationFlagsType) -> Self {
51+
*val
52+
}
53+
}
54+
55+
56+
impl Session {
57+
/// Get requested validation flags from the session
58+
///
59+
/// The only supported flag as for PKCS#11 3.2 is `ValidationFlagsType::VALIDATION_OK`
60+
pub fn get_validation_flags(&self, flags_type: ValidationFlagsType
61+
) -> Result<CK_FLAGS> {
62+
let mut flags: CK_FLAGS = 0;
63+
unsafe {
64+
Rv::from(get_pkcs11!(self.client(), C_GetSessionValidationFlags)(
65+
self.handle(),
66+
flags_type.into(),
67+
&mut flags,
68+
))
69+
.into_result(Function::GetSessionValidationFlags)?;
70+
}
71+
Ok(flags)
72+
}
73+
}

0 commit comments

Comments
 (0)