-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathcreate_command_output.rs
More file actions
96 lines (80 loc) · 3.72 KB
/
create_command_output.rs
File metadata and controls
96 lines (80 loc) · 3.72 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
// Copyright 2022 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use crate::{
ffi::take_from_esys,
structures::{CreateKeyResult, CreationData, CreationTicket, Digest, Private, Public},
tss2_esys::{TPM2B_CREATION_DATA, TPM2B_DIGEST, TPM2B_PRIVATE, TPM2B_PUBLIC, TPMT_TK_CREATION},
Error, Result,
};
use std::convert::TryFrom;
use std::ptr::null_mut;
/// Struct that handles the output of the
/// Create Esys_Create command and zeroizes
/// the FFI data.
pub(crate) struct CreateCommandOutputHandler {
ffi_out_public_ptr: *mut TPM2B_PUBLIC,
ffi_out_private_ptr: *mut TPM2B_PRIVATE,
ffi_creation_data_ptr: *mut TPM2B_CREATION_DATA,
ffi_creation_hash_ptr: *mut TPM2B_DIGEST,
ffi_creation_ticket_ptr: *mut TPMT_TK_CREATION,
}
/// Creates a new CreateCommandOutputHandler
impl CreateCommandOutputHandler {
pub(crate) fn new() -> Self {
Self {
ffi_out_private_ptr: null_mut(),
ffi_out_public_ptr: null_mut(),
ffi_creation_data_ptr: null_mut(),
ffi_creation_hash_ptr: null_mut(),
ffi_creation_ticket_ptr: null_mut(),
}
}
/// A reference to the where 'outPrivate' output parameter pointer shall be stored.
pub fn ffi_out_private_ptr(&mut self) -> &mut *mut TPM2B_PRIVATE {
&mut self.ffi_out_private_ptr
}
/// A reference to the where 'outPublic' output parameter pointer shall be stored.
pub fn ffi_out_public_ptr(&mut self) -> &mut *mut TPM2B_PUBLIC {
&mut self.ffi_out_public_ptr
}
/// A reference to the where 'creationData' output parameter pointer shall be stored.
pub fn ffi_creation_data_ptr(&mut self) -> &mut *mut TPM2B_CREATION_DATA {
&mut self.ffi_creation_data_ptr
}
/// A reference to the where 'creationHash' output parameter pointer shall be stored.
pub fn ffi_creation_hash_ptr(&mut self) -> &mut *mut TPM2B_DIGEST {
&mut self.ffi_creation_hash_ptr
}
/// A reference to the where 'creationTicket' output parameter pointer shall be stored.
pub fn ffi_creation_ticket_ptr(&mut self) -> &mut *mut TPMT_TK_CREATION {
&mut self.ffi_creation_ticket_ptr
}
}
impl TryFrom<CreateCommandOutputHandler> for CreateKeyResult {
type Error = Error;
fn try_from(mut ffi_data_handler: CreateCommandOutputHandler) -> Result<CreateKeyResult> {
// Take and free with Esys_Free; then null out the handler's fields so Drop (if any)
// won't free them a second time.
let out_private_owned = unsafe { take_from_esys(ffi_data_handler.ffi_out_private_ptr)? };
ffi_data_handler.ffi_out_private_ptr = null_mut();
let out_public_owned = unsafe { take_from_esys(ffi_data_handler.ffi_out_public_ptr)? };
ffi_data_handler.ffi_out_public_ptr = null_mut();
let creation_data_owned =
unsafe { take_from_esys(ffi_data_handler.ffi_creation_data_ptr)? };
ffi_data_handler.ffi_creation_data_ptr = null_mut();
let creation_hash_owned =
unsafe { take_from_esys(ffi_data_handler.ffi_creation_hash_ptr)? };
ffi_data_handler.ffi_creation_hash_ptr = null_mut();
let creation_ticket_owned =
unsafe { take_from_esys(ffi_data_handler.ffi_creation_ticket_ptr)? };
//#[allow(unused_assignments)]
//ffi_data_handler.ffi_creation_ticket_ptr = null_mut();
Ok(CreateKeyResult {
out_private: Private::try_from(out_private_owned)?,
out_public: Public::try_from(out_public_owned)?,
creation_data: CreationData::try_from(creation_data_owned)?,
creation_hash: Digest::try_from(creation_hash_owned)?,
creation_ticket: CreationTicket::try_from(creation_ticket_owned)?,
})
}
}