forked from parallaxsecond/rust-cryptoki
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
111 lines (97 loc) · 2.75 KB
/
mod.rs
File metadata and controls
111 lines (97 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
//! Session types
use crate::context::Pkcs11;
use cryptoki_sys::*;
use std::fmt::Formatter;
use std::marker::PhantomData;
mod decryption;
mod digesting;
mod encapsulation;
mod encryption;
mod key_management;
mod message_decryption;
mod message_encryption;
mod object_management;
mod random;
mod session_info;
mod session_management;
mod signing_macing;
mod slot_token_management;
mod validation;
pub use object_management::ObjectHandleIterator;
pub use session_info::{SessionInfo, SessionState};
pub use validation::ValidationFlagsType;
/// Type that identifies a session
///
/// It will automatically get closed (and logout) on drop.
/// Session does not implement Sync to prevent the same Session instance to be used from multiple
/// threads. A Session needs to be created in its own thread or to be passed by ownership to
/// another thread.
#[derive(Debug)]
pub struct Session<'a> {
handle: CK_SESSION_HANDLE,
client: &'a Pkcs11,
// This is not used but to prevent Session to automatically implement Send and Sync
_guard: PhantomData<*mut u32>,
}
impl<'a> std::fmt::Display for Session<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.handle)
}
}
impl<'a> std::fmt::LowerHex for Session<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:08x}", self.handle)
}
}
impl<'a> std::fmt::UpperHex for Session<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:08X}", self.handle)
}
}
impl<'a> Session<'a> {
pub(crate) fn new(handle: CK_SESSION_HANDLE, client: &'a Pkcs11) -> Self {
Session {
handle,
client,
_guard: PhantomData,
}
}
}
impl<'a> Session<'a> {
/// Close a session
/// This will be called on drop as well.
pub fn close(self) {}
/// Get the raw handle of the session.
pub fn handle(&self) -> CK_SESSION_HANDLE {
self.handle
}
pub(crate) fn client(&self) -> &Pkcs11 {
self.client
}
}
/// Types of PKCS11 users
#[derive(Copy, Clone, Debug)]
pub enum UserType {
/// Security Officer
So,
/// User
User,
/// Context Specific
ContextSpecific,
/// Vendor extension
VendorExtension(u32),
}
impl From<UserType> for CK_USER_TYPE {
// Mask lint for n.into() on 32-bit systems.
#![allow(clippy::useless_conversion)]
fn from(user_type: UserType) -> CK_USER_TYPE {
match user_type {
UserType::So => CKU_SO,
UserType::User => CKU_USER,
UserType::ContextSpecific => CKU_CONTEXT_SPECIFIC,
UserType::VendorExtension(n) => n.into(),
}
}
}