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
use crate::get_pkcs11;
use crate::types::function::Rv;
use crate::types::mechanism::Mechanism;
use crate::types::object::{Attribute, ObjectHandle};
use crate::types::session::Session;
use crate::Result;
use cryptoki_sys::{CK_ATTRIBUTE, CK_MECHANISM, CK_MECHANISM_PTR};
use std::convert::TryInto;
impl<'a> Session<'a> {
pub fn generate_key_pair(
&self,
mechanism: &Mechanism,
pub_key_template: &[Attribute],
priv_key_template: &[Attribute],
) -> Result<(ObjectHandle, ObjectHandle)> {
let mut mechanism: CK_MECHANISM = mechanism.into();
let mut pub_key_template: Vec<CK_ATTRIBUTE> =
pub_key_template.iter().map(|attr| attr.into()).collect();
let mut priv_key_template: Vec<CK_ATTRIBUTE> =
priv_key_template.iter().map(|attr| attr.into()).collect();
let mut pub_handle = 0;
let mut priv_handle = 0;
unsafe {
Rv::from(get_pkcs11!(self.client(), C_GenerateKeyPair)(
self.handle(),
&mut mechanism as CK_MECHANISM_PTR,
pub_key_template.as_mut_ptr(),
pub_key_template.len().try_into()?,
priv_key_template.as_mut_ptr(),
priv_key_template.len().try_into()?,
&mut pub_handle,
&mut priv_handle,
))
.into_result()?;
}
Ok((
ObjectHandle::new(pub_handle),
ObjectHandle::new(priv_handle),
))
}
}