|
| 1 | +// Copyright 2024 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +use crate::{ |
| 4 | + handles::TpmHandle, |
| 5 | + interface_types::{data_handles::Saved, reserved_handles::Hierarchy}, |
| 6 | + structures::TpmContextData, |
| 7 | + traits::impl_mu_standard, |
| 8 | + traits::{Marshall, UnMarshall}, |
| 9 | + tss2_esys::TPMS_CONTEXT, |
| 10 | + Error, Result, |
| 11 | +}; |
| 12 | +use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 13 | +use std::convert::TryFrom; |
| 14 | + |
| 15 | +/// Structure holding the content of a TPM context. |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +pub struct SavedTpmContext { |
| 18 | + sequence: u64, |
| 19 | + saved_handle: Saved, |
| 20 | + hierarchy: Hierarchy, |
| 21 | + context_blob: TpmContextData, |
| 22 | +} |
| 23 | + |
| 24 | +impl SavedTpmContext { |
| 25 | + /// The sequence parameter |
| 26 | + /// |
| 27 | + /// # Details |
| 28 | + /// "The sequence parameter is used to differentiate the contexts and to allow the TPM to create a different |
| 29 | + /// encryption key for each context." |
| 30 | + pub const fn sequence(&self) -> u64 { |
| 31 | + self.sequence |
| 32 | + } |
| 33 | + |
| 34 | + /// The saved handle. |
| 35 | + pub const fn saved_handle(&self) -> Saved { |
| 36 | + self.saved_handle |
| 37 | + } |
| 38 | + |
| 39 | + /// The hierarchy for the saved context. |
| 40 | + pub const fn hierarchy(&self) -> Hierarchy { |
| 41 | + self.hierarchy |
| 42 | + } |
| 43 | + |
| 44 | + /// The context blob. |
| 45 | + /// |
| 46 | + /// # Details |
| 47 | + /// "This is the hierarchy ([Hierarchy]) for the saved context and determines the proof value used |
| 48 | + /// in the construction of the encryption and integrity values for the context. For session and sequence |
| 49 | + /// contexts, the hierarchy is [Hierarchy::Null]. The hierarchy for a transient object may be [Hierarchy::Null] |
| 50 | + /// but it is not required." |
| 51 | + pub fn context_blob(&self) -> &TpmContextData { |
| 52 | + &self.context_blob |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl TryFrom<TPMS_CONTEXT> for SavedTpmContext { |
| 57 | + type Error = Error; |
| 58 | + |
| 59 | + fn try_from(tss: TPMS_CONTEXT) -> Result<SavedTpmContext> { |
| 60 | + Ok(SavedTpmContext { |
| 61 | + sequence: tss.sequence, |
| 62 | + saved_handle: Saved::try_from(tss.savedHandle)?, |
| 63 | + hierarchy: TpmHandle::try_from(tss.hierarchy).and_then(Hierarchy::try_from)?, |
| 64 | + context_blob: TpmContextData::try_from(tss.contextBlob)?, |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl From<SavedTpmContext> for TPMS_CONTEXT { |
| 70 | + fn from(native: SavedTpmContext) -> TPMS_CONTEXT { |
| 71 | + TPMS_CONTEXT { |
| 72 | + sequence: native.sequence, |
| 73 | + savedHandle: native.saved_handle.into(), |
| 74 | + hierarchy: TpmHandle::from(native.hierarchy).into(), |
| 75 | + contextBlob: native.context_blob.into(), |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl_mu_standard!(SavedTpmContext, TPMS_CONTEXT); |
| 81 | + |
| 82 | +impl Serialize for SavedTpmContext { |
| 83 | + /// Serialize the [SavedTpmContext] data into it's bytes representation of the TCG |
| 84 | + /// TPMS_CONTEXT structure. |
| 85 | + fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
| 86 | + where |
| 87 | + S: Serializer, |
| 88 | + { |
| 89 | + let bytes = self.marshall().map_err(serde::ser::Error::custom)?; |
| 90 | + serializer.serialize_bytes(&bytes) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<'de> Deserialize<'de> for SavedTpmContext { |
| 95 | + /// Deserialize the [SavedTpmContext] data from it's bytes representation of the TCG |
| 96 | + /// TPMS_CONTEXT structure. |
| 97 | + fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> |
| 98 | + where |
| 99 | + D: Deserializer<'de>, |
| 100 | + { |
| 101 | + let bytes = <Vec<u8>>::deserialize(deserializer)?; |
| 102 | + Self::unmarshall(&bytes).map_err(serde::de::Error::custom) |
| 103 | + } |
| 104 | +} |
0 commit comments