Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions tss-esapi/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use handle_manager::HandleManager;
use log::{debug, error};
use malloced::Malloced;
use std::collections::HashMap;
use std::ptr::null_mut;
use std::{ffi::c_void, ptr, ptr::null_mut};

/// Safe abstraction over an ESYS_CONTEXT.
///
Expand Down Expand Up @@ -454,12 +454,16 @@ impl Context {

/// Private function for handling that has been allocated with
/// C memory allocation functions in TSS.
fn ffi_data_to_owned<T: Copy>(data_ptr: *mut T) -> T {
let out = unsafe { *data_ptr };
fn ffi_data_to_owned<T: Copy>(data_ptr: *mut T) -> Result<T> {
if data_ptr.is_null() {
error!("Null pointer received from TSS");
return Err(Error::local_error(ErrorKind::WrongValueFromTpm));
}

let out = unsafe { ptr::read(data_ptr) };
unsafe { Esys_Free(data_ptr.cast::<c_void>()) };

// Free the malloced data.
drop(unsafe { Malloced::from_raw(data_ptr) });
out
Ok(out)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tss-esapi/src/context/general_esys_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Context {
error!("Error in getting name: {:#010X}", ret);
},
)?;
Name::try_from(Context::ffi_data_to_owned(name_ptr))
Name::try_from(Context::ffi_data_to_owned(name_ptr)?)
}

/// Used to construct an esys object from the resources inside the TPM.
Expand Down
10 changes: 5 additions & 5 deletions tss-esapi/src/context/tpm_commands/asymmetric_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Context {
error!("Error when performing RSA encryption: {:#010X}", ret);
},
)?;
PublicKeyRsa::try_from(Context::ffi_data_to_owned(out_data_ptr))
PublicKeyRsa::try_from(Context::ffi_data_to_owned(out_data_ptr)?)
}

/// Perform an asymmetric RSA decryption.
Expand Down Expand Up @@ -69,7 +69,7 @@ impl Context {
error!("Error when performing RSA decryption: {:#010X}", ret);
},
)?;
PublicKeyRsa::try_from(Context::ffi_data_to_owned(message_ptr))
PublicKeyRsa::try_from(Context::ffi_data_to_owned(message_ptr)?)
}

/// Generate an ephemeral key pair.
Expand Down Expand Up @@ -199,8 +199,8 @@ impl Context {
},
)?;

let z_point = Context::ffi_data_to_owned(z_point_ptr);
let pub_point = Context::ffi_data_to_owned(pub_point_ptr);
let z_point = Context::ffi_data_to_owned(z_point_ptr)?;
let pub_point = Context::ffi_data_to_owned(pub_point_ptr)?;
Ok((
EccPoint::try_from(z_point.point)?,
EccPoint::try_from(pub_point.point)?,
Expand Down Expand Up @@ -335,7 +335,7 @@ impl Context {
error!("Error when performing ECDH ZGen: {:#010X}", ret);
},
)?;
let out_point = Context::ffi_data_to_owned(out_point_ptr);
let out_point = Context::ffi_data_to_owned(out_point_ptr)?;
EccPoint::try_from(out_point.point)
}

Expand Down
16 changes: 8 additions & 8 deletions tss-esapi/src/context/tpm_commands/attestation_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ impl Context {
},
)?;

let certify_info = Context::ffi_data_to_owned(certify_info_ptr);
let signature = Context::ffi_data_to_owned(signature_ptr);
let certify_info = Context::ffi_data_to_owned(certify_info_ptr)?;
let signature = Context::ffi_data_to_owned(signature_ptr)?;
Ok((
Attest::try_from(AttestBuffer::try_from(certify_info)?)?,
Signature::try_from(signature)?,
Expand Down Expand Up @@ -272,8 +272,8 @@ impl Context {
},
)?;

let certify_info = Context::ffi_data_to_owned(certify_info_ptr);
let signature = Context::ffi_data_to_owned(signature_ptr);
let certify_info = Context::ffi_data_to_owned(certify_info_ptr)?;
let signature = Context::ffi_data_to_owned(signature_ptr)?;
Ok((
Attest::try_from(AttestBuffer::try_from(certify_info)?)?,
Signature::try_from(signature)?,
Expand Down Expand Up @@ -313,8 +313,8 @@ impl Context {
},
)?;

let quoted = Context::ffi_data_to_owned(quoted_ptr);
let signature = Context::ffi_data_to_owned(signature_ptr);
let quoted = Context::ffi_data_to_owned(quoted_ptr)?;
let signature = Context::ffi_data_to_owned(signature_ptr)?;
Ok((
Attest::try_from(AttestBuffer::try_from(quoted)?)?,
Signature::try_from(signature)?,
Expand Down Expand Up @@ -426,8 +426,8 @@ impl Context {
},
)?;

let timeinfo = Context::ffi_data_to_owned(timeinfo_ptr);
let signature = Context::ffi_data_to_owned(signature_ptr);
let timeinfo = Context::ffi_data_to_owned(timeinfo_ptr)?;
let signature = Context::ffi_data_to_owned(signature_ptr)?;
Ok((
Attest::try_from(AttestBuffer::try_from(timeinfo)?)?,
Signature::try_from(signature)?,
Expand Down
2 changes: 1 addition & 1 deletion tss-esapi/src/context/tpm_commands/capability_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Context {
)?;

Ok((
CapabilityData::try_from(Context::ffi_data_to_owned(capability_data_ptr))?,
CapabilityData::try_from(Context::ffi_data_to_owned(capability_data_ptr)?)?,
YesNo::try_from(more_data)?.into(),
))
}
Expand Down
2 changes: 1 addition & 1 deletion tss-esapi/src/context/tpm_commands/context_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Context {
error!("Error in saving context: {:#010X}", ret);
},
)?;
SavedTpmContext::try_from(Context::ffi_data_to_owned(context_ptr))
SavedTpmContext::try_from(Context::ffi_data_to_owned(context_ptr)?)
}

/// Load a previously saved context into the TPM and return the object handle.
Expand Down
8 changes: 4 additions & 4 deletions tss-esapi/src/context/tpm_commands/duplication_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ impl Context {
)?;

Ok((
Data::try_from(Context::ffi_data_to_owned(encryption_key_out_ptr))?,
Private::try_from(Context::ffi_data_to_owned(duplicate_ptr))?,
EncryptedSecret::try_from(Context::ffi_data_to_owned(out_sym_seed_ptr))?,
Data::try_from(Context::ffi_data_to_owned(encryption_key_out_ptr)?)?,
Private::try_from(Context::ffi_data_to_owned(duplicate_ptr)?)?,
EncryptedSecret::try_from(Context::ffi_data_to_owned(out_sym_seed_ptr)?)?,
))
}

Expand Down Expand Up @@ -683,6 +683,6 @@ impl Context {
error!("Error when performing import: {:#010X}", ret);
},
)?;
Private::try_from(Context::ffi_data_to_owned(out_private_ptr))
Private::try_from(Context::ffi_data_to_owned(out_private_ptr)?)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ impl Context {
},
)?;
Ok((
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr))?,
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr))?,
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr)?)?,
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr)?)?,
))
}

Expand Down Expand Up @@ -106,8 +106,8 @@ impl Context {
},
)?;
Ok((
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr))?,
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr))?,
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr)?)?,
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr)?)?,
))
}

Expand Down Expand Up @@ -533,7 +533,7 @@ impl Context {
},
)?;

Digest::try_from(Context::ffi_data_to_owned(policy_digest_ptr))
Digest::try_from(Context::ffi_data_to_owned(policy_digest_ptr)?)
}

/// Cause conditional gating of a policy based on NV written state.
Expand Down
8 changes: 4 additions & 4 deletions tss-esapi/src/context/tpm_commands/hierarchy_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ impl Context {
error!("Error in creating primary key: {:#010X}", ret);
},
)?;
let out_public_owned = Context::ffi_data_to_owned(out_public_ptr);
let creation_data_owned = Context::ffi_data_to_owned(creation_data_ptr);
let creation_hash_owned = Context::ffi_data_to_owned(creation_hash_ptr);
let creation_ticket_owned = Context::ffi_data_to_owned(creation_ticket_ptr);
let out_public_owned = Context::ffi_data_to_owned(out_public_ptr)?;
let creation_data_owned = Context::ffi_data_to_owned(creation_data_ptr)?;
let creation_hash_owned = Context::ffi_data_to_owned(creation_hash_ptr)?;
let creation_ticket_owned = Context::ffi_data_to_owned(creation_ticket_ptr)?;
let primary_key_handle = KeyHandle::from(object_handle);
self.handle_manager
.add_handle(primary_key_handle.into(), HandleDropAction::Flush)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ impl Context {

Ok((
pcr_update_counter,
PcrSelectionList::try_from(Context::ffi_data_to_owned(pcr_selection_out_ptr))?,
DigestList::try_from(Context::ffi_data_to_owned(pcr_values_ptr))?,
PcrSelectionList::try_from(Context::ffi_data_to_owned(pcr_selection_out_ptr)?)?,
DigestList::try_from(Context::ffi_data_to_owned(pcr_values_ptr)?)?,
))
}

Expand Down
6 changes: 3 additions & 3 deletions tss-esapi/src/context/tpm_commands/non_volatile_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ impl Context {
)?;

Ok((
NvPublic::try_from(Context::ffi_data_to_owned(nv_public_ptr))?,
Name::try_from(Context::ffi_data_to_owned(nv_name_ptr))?,
NvPublic::try_from(Context::ffi_data_to_owned(nv_public_ptr)?)?,
Name::try_from(Context::ffi_data_to_owned(nv_name_ptr)?)?,
))
}

Expand Down Expand Up @@ -825,7 +825,7 @@ impl Context {
error!("Error when reading NV: {:#010X}", ret);
},
)?;
MaxNvBuffer::try_from(Context::ffi_data_to_owned(data_ptr))
MaxNvBuffer::try_from(Context::ffi_data_to_owned(data_ptr)?)
}

// Missing function: NV_ReadLock
Expand Down
16 changes: 8 additions & 8 deletions tss-esapi/src/context/tpm_commands/object_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ impl Context {
},
)?;
Ok((
Public::try_from(Context::ffi_data_to_owned(out_public_ptr))?,
Name::try_from(Context::ffi_data_to_owned(name_ptr))?,
Name::try_from(Context::ffi_data_to_owned(qualified_name_ptr))?,
Public::try_from(Context::ffi_data_to_owned(out_public_ptr)?)?,
Name::try_from(Context::ffi_data_to_owned(name_ptr)?)?,
Name::try_from(Context::ffi_data_to_owned(qualified_name_ptr)?)?,
))
}

Expand Down Expand Up @@ -250,7 +250,7 @@ impl Context {
},
)?;

Digest::try_from(Context::ffi_data_to_owned(cert_info_ptr))
Digest::try_from(Context::ffi_data_to_owned(cert_info_ptr)?)
}

/// Perform actions to create a [IdObject] containing an activation credential.
Expand Down Expand Up @@ -283,8 +283,8 @@ impl Context {
},
)?;
Ok((
IdObject::try_from(Context::ffi_data_to_owned(credential_blob_ptr))?,
EncryptedSecret::try_from(Context::ffi_data_to_owned(secret_ptr))?,
IdObject::try_from(Context::ffi_data_to_owned(credential_blob_ptr)?)?,
EncryptedSecret::try_from(Context::ffi_data_to_owned(secret_ptr)?)?,
))
}

Expand All @@ -307,7 +307,7 @@ impl Context {
error!("Error in unsealing: {:#010X}", ret);
},
)?;
SensitiveData::try_from(Context::ffi_data_to_owned(out_data_ptr))
SensitiveData::try_from(Context::ffi_data_to_owned(out_data_ptr)?)
}

/// Change authorization for a TPM-resident object.
Expand Down Expand Up @@ -335,7 +335,7 @@ impl Context {
error!("Error changing object auth: {:#010X}", ret);
},
)?;
Private::try_from(Context::ffi_data_to_owned(out_private_ptr))
Private::try_from(Context::ffi_data_to_owned(out_private_ptr)?)
}

// Missing function: CreateLoaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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,
Expand Down Expand Up @@ -61,17 +62,28 @@ impl CreateCommandOutputHandler {
impl TryFrom<CreateCommandOutputHandler> for CreateKeyResult {
type Error = Error;

fn try_from(ffi_data_handler: CreateCommandOutputHandler) -> Result<CreateKeyResult> {
let out_private_owned =
crate::ffi::to_owned_with_zeroized_source(ffi_data_handler.ffi_out_private_ptr);
let out_public_owned =
crate::ffi::to_owned_with_zeroized_source(ffi_data_handler.ffi_out_public_ptr);
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 =
crate::ffi::to_owned_with_zeroized_source(ffi_data_handler.ffi_creation_data_ptr);
unsafe { take_from_esys(ffi_data_handler.ffi_creation_data_ptr)? };
ffi_data_handler.ffi_creation_data_ptr = null_mut();

let creation_hash_owned =
crate::ffi::to_owned_with_zeroized_source(ffi_data_handler.ffi_creation_hash_ptr);
unsafe { take_from_esys(ffi_data_handler.ffi_creation_hash_ptr)? };
ffi_data_handler.ffi_creation_hash_ptr = null_mut();

let creation_ticket_owned =
crate::ffi::to_owned_with_zeroized_source(ffi_data_handler.ffi_creation_ticket_ptr);
unsafe { take_from_esys(ffi_data_handler.ffi_creation_ticket_ptr)? };
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)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Context {
error!("Error in getting random bytes: {:#010X}", ret);
},
)?;
Digest::try_from(Context::ffi_data_to_owned(random_bytes_ptr))
Digest::try_from(Context::ffi_data_to_owned(random_bytes_ptr)?)
}

/// Add additional information into the TPM RNG state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Context {
error!("Error when verifying signature: {:#010X}", ret);
},
)?;
VerifiedTicket::try_from(Context::ffi_data_to_owned(validation_ptr))
VerifiedTicket::try_from(Context::ffi_data_to_owned(validation_ptr)?)
}

/// Sign a digest with a key present in the TPM and return the signature.
Expand Down Expand Up @@ -118,6 +118,6 @@ impl Context {
error!("Error when signing: {:#010X}", ret);
},
)?;
Signature::try_from(Context::ffi_data_to_owned(signature_ptr))
Signature::try_from(Context::ffi_data_to_owned(signature_ptr)?)
}
}
10 changes: 5 additions & 5 deletions tss-esapi/src/context/tpm_commands/symmetric_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ impl Context {
},
)?;
Ok((
MaxBuffer::try_from(Context::ffi_data_to_owned(out_data_ptr))?,
InitialValue::try_from(Context::ffi_data_to_owned(iv_out_ptr))?,
MaxBuffer::try_from(Context::ffi_data_to_owned(out_data_ptr)?)?,
InitialValue::try_from(Context::ffi_data_to_owned(iv_out_ptr)?)?,
))
}

Expand Down Expand Up @@ -290,8 +290,8 @@ impl Context {
},
)?;
Ok((
Digest::try_from(Context::ffi_data_to_owned(out_hash_ptr))?,
HashcheckTicket::try_from(Context::ffi_data_to_owned(validation_ptr))?,
Digest::try_from(Context::ffi_data_to_owned(out_hash_ptr)?)?,
HashcheckTicket::try_from(Context::ffi_data_to_owned(validation_ptr)?)?,
))
}

Expand Down Expand Up @@ -369,7 +369,7 @@ impl Context {
error!("Error in hmac: {:#010X}", ret);
},
)?;
Digest::try_from(Context::ffi_data_to_owned(out_hmac_ptr))
Digest::try_from(Context::ffi_data_to_owned(out_hmac_ptr)?)
}

// Missing function: MAC
Expand Down
2 changes: 1 addition & 1 deletion tss-esapi/src/context/tpm_commands/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Context {
},
)?;
Ok((
MaxBuffer::try_from(Context::ffi_data_to_owned(out_data_ptr))?,
MaxBuffer::try_from(Context::ffi_data_to_owned(out_data_ptr)?)?,
ReturnCode::ensure_success(test_result, |_| {}),
))
}
Expand Down
Loading