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
112
113
use crate::get_pkcs11;
use crate::types::function::Rv;
use crate::types::slot_token::Slot;
use crate::Pkcs11;
use crate::Result;
use crate::Session;
use secrecy::{ExposeSecret, Secret};
use std::convert::TryInto;
use std::ffi::CString;
impl Pkcs11 {
pub fn get_slots_with_token(&self) -> Result<Vec<Slot>> {
let mut slot_count = 0;
unsafe {
Rv::from(get_pkcs11!(self, C_GetSlotList)(
cryptoki_sys::CK_TRUE,
std::ptr::null_mut(),
&mut slot_count,
))
.into_result()?;
}
let mut slots = vec![0; slot_count.try_into()?];
unsafe {
Rv::from(get_pkcs11!(self, C_GetSlotList)(
cryptoki_sys::CK_TRUE,
slots.as_mut_ptr(),
&mut slot_count,
))
.into_result()?;
}
let mut slots: Vec<Slot> = slots.into_iter().map(Slot::new).collect();
slots.resize(slot_count.try_into()?, Slot::new(0));
Ok(slots)
}
pub fn get_all_slots(&self) -> Result<Vec<Slot>> {
let mut slot_count = 0;
unsafe {
Rv::from(get_pkcs11!(self, C_GetSlotList)(
cryptoki_sys::CK_FALSE,
std::ptr::null_mut(),
&mut slot_count,
))
.into_result()?;
}
let mut slots = vec![0; slot_count.try_into()?];
unsafe {
Rv::from(get_pkcs11!(self, C_GetSlotList)(
cryptoki_sys::CK_FALSE,
slots.as_mut_ptr(),
&mut slot_count,
))
.into_result()?;
}
let mut slots: Vec<Slot> = slots.into_iter().map(Slot::new).collect();
slots.resize(slot_count.try_into()?, Slot::new(0));
Ok(slots)
}
pub fn init_token(&self, slot: Slot, pin: &str) -> Result<()> {
let pin = Secret::new(CString::new(pin)?.into_bytes());
let label = [b' '; 32];
unsafe {
Rv::from(get_pkcs11!(self, C_InitToken)(
slot.into(),
pin.expose_secret().as_ptr() as *mut u8,
pin.expose_secret().len().try_into()?,
label.as_ptr() as *mut u8,
))
.into_result()
}
}
}
impl<'a> Session<'a> {
pub fn init_pin(&self, pin: &str) -> Result<()> {
let pin = Secret::new(CString::new(pin)?.into_bytes());
unsafe {
Rv::from(get_pkcs11!(self.client(), C_InitPIN)(
self.handle(),
pin.expose_secret().as_ptr() as *mut u8,
pin.expose_secret().len().try_into()?,
))
.into_result()
}
}
}