|
| 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