|
1 | | -// Copyright 2020 Contributors to the Parsec project. |
2 | | -// SPDX-License-Identifier: Apache-2.0 |
3 | | - |
4 | | -use crate::handles::{NvIndexTpmHandle, PcrTpmHandle, PersistentTpmHandle, TransientTpmHandle}; |
5 | | - |
6 | | -/// Can be created with either a persistent |
7 | | -/// or transient TPM handle. |
| 1 | +/// This module contains native representations of the TPMI_DH types. |
| 2 | +use crate::{ |
| 3 | + handles::{ |
| 4 | + HmacSessionTpmHandle, NvIndexTpmHandle, PcrTpmHandle, PersistentTpmHandle, |
| 5 | + PolicySessionTpmHandle, TpmHandle, TransientTpmHandle, |
| 6 | + }, |
| 7 | + tss2_esys::TPMI_DH_CONTEXT, |
| 8 | + Error, Result, WrapperErrorKind, |
| 9 | +}; |
| 10 | +use std::convert::TryFrom; |
| 11 | +/// Enum representing the 'Object' data handles interface type. |
| 12 | +/// |
| 13 | +/// # Details |
| 14 | +/// This corresponds to the TPMI_DH_OBJECT interface type. |
8 | 15 | #[derive(Debug, Copy, Clone)] |
9 | 16 | pub enum Object { |
10 | 17 | Transient(TransientTpmHandle), |
@@ -53,10 +60,52 @@ pub enum Entity { |
53 | 60 | Platform, |
54 | 61 | Endorsement, |
55 | 62 | Lockout, |
56 | | - // TODO: Handle Auth |
| 63 | + // TODO: Handle Auth, that is vendor specific. |
57 | 64 | } |
58 | 65 |
|
59 | 66 | #[derive(Debug, Copy, Clone)] |
60 | 67 | pub enum Pcr { |
61 | 68 | Pcr(PcrTpmHandle), |
62 | 69 | } |
| 70 | + |
| 71 | +/// Enum representing the 'Context' data handles interface type. |
| 72 | +/// |
| 73 | +/// # Details |
| 74 | +/// This corresponds to the TPMI_DH_CONTEXT interface type. |
| 75 | +#[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 76 | +pub enum ContextDataHandle { |
| 77 | + Hmac(HmacSessionTpmHandle), |
| 78 | + Policy(PolicySessionTpmHandle), |
| 79 | + Transient(TransientTpmHandle), |
| 80 | +} |
| 81 | + |
| 82 | +impl From<HmacSessionTpmHandle> for ContextDataHandle { |
| 83 | + fn from(hmac_session_tpm_handle: HmacSessionTpmHandle) -> Self { |
| 84 | + ContextDataHandle::Hmac(hmac_session_tpm_handle) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl From<PolicySessionTpmHandle> for ContextDataHandle { |
| 89 | + fn from(policy_session_tpm_handle: PolicySessionTpmHandle) -> Self { |
| 90 | + ContextDataHandle::Policy(policy_session_tpm_handle) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl From<TransientTpmHandle> for ContextDataHandle { |
| 95 | + fn from(transient_tpm_handle: TransientTpmHandle) -> Self { |
| 96 | + ContextDataHandle::Transient(transient_tpm_handle) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl TryFrom<TPMI_DH_CONTEXT> for ContextDataHandle { |
| 101 | + type Error = Error; |
| 102 | + |
| 103 | + fn try_from(ffi: TPMI_DH_CONTEXT) -> Result<Self> { |
| 104 | + TpmHandle::try_from(ffi).and_then(|tpm_handle| match tpm_handle { |
| 105 | + TpmHandle::HmacSession(handle) => Ok(Self::Hmac(handle)), |
| 106 | + TpmHandle::PolicySession(handle) => Ok(Self::Policy(handle)), |
| 107 | + TpmHandle::Transient(handle) => Ok(Self::Transient(handle)), |
| 108 | + _ => Err(Error::local_error(WrapperErrorKind::InvalidParam)), |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
0 commit comments