Skip to content

Commit b1325a6

Browse files
committed
Restructure NvOpenOptions
Changing `NvOpenOptions` to reflect the different paths available for specifying the NV index. Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent a301e33 commit b1325a6

File tree

2 files changed

+44
-45
lines changed
  • tss-esapi

2 files changed

+44
-45
lines changed

tss-esapi/src/abstraction/nv.rs

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ pub fn read_full(
2020
auth_handle: NvAuth,
2121
nv_index_handle: NvIndexTpmHandle,
2222
) -> Result<Vec<u8>> {
23-
let mut rw = NvOpenOptions::new().open(context, auth_handle, nv_index_handle)?;
23+
let mut rw = NvOpenOptions::Index {
24+
auth_handle,
25+
nv_index_handle,
26+
}
27+
.open(context)?;
2428
let mut result = Vec::with_capacity(rw.size());
2529

2630
let _ = rw.read_to_end(&mut result).map_err(|e| {
@@ -80,47 +84,39 @@ pub fn list(context: &mut Context) -> Result<Vec<(NvPublic, Name)>> {
8084
}
8185

8286
/// Options and flags which can be used to determine how a non-volatile storage index is opened.
83-
///
84-
/// This builder exposes the ability to determine how a [`NvReaderWriter`] is opened, and is typically used by
85-
/// calling [`NvOpenOptions::new`], chaining method calls to set each option and then calling [`NvOpenOptions::open`].
86-
#[derive(Debug, Clone, Default)]
87-
pub struct NvOpenOptions {
88-
nv_public: Option<NvPublic>,
87+
#[derive(Debug, Clone)]
88+
pub enum NvOpenOptions {
89+
Public {
90+
nv_public: NvPublic,
91+
auth_handle: NvAuth,
92+
},
93+
Index {
94+
nv_index_handle: NvIndexTpmHandle,
95+
auth_handle: NvAuth,
96+
},
8997
}
9098

9199
impl NvOpenOptions {
92-
/// Creates a new blank set of options for opening a non-volatile storage index
93-
///
94-
/// All options are initially set to `false`/`None`.
95-
pub fn new() -> Self {
96-
Self { nv_public: None }
97-
}
98-
99-
/// Sets the public attributes to use when creating the non-volatile storage index
100-
///
101-
/// If the public attributes are `None` then the non-volatile storage index will be opened or otherwise
102-
/// it will be created.
103-
pub fn with_nv_public(&mut self, nv_public: Option<NvPublic>) -> &mut Self {
104-
self.nv_public = nv_public;
105-
self
106-
}
107-
108100
/// Opens a non-volatile storage index using the options specified by `self`
109101
///
110102
/// The non-volatile storage index may be used for reading or writing or both.
111-
pub fn open<'a>(
112-
&self,
113-
context: &'a mut Context,
114-
auth_handle: NvAuth,
115-
nv_index_handle: NvIndexTpmHandle,
116-
) -> Result<NvReaderWriter<'a>> {
103+
pub fn open<'a>(&self, context: &'a mut Context) -> Result<NvReaderWriter<'a>> {
117104
let buffer_size = context
118105
.get_tpm_property(PropertyTag::NvBufferMax)?
119-
.unwrap_or(MaxNvBuffer::MAX_SIZE as u32) as usize;
106+
.map(usize::try_from)
107+
.transpose()
108+
.map_err(|_| {
109+
log::error!("Failed to obtain valid maximum NV buffer size");
110+
Error::WrapperError(WrapperErrorKind::InternalError)
111+
})?
112+
.unwrap_or(MaxNvBuffer::MAX_SIZE);
120113

121-
let (data_size, nv_idx) = match &self.nv_public {
122-
None => {
123-
let nv_idx = TpmHandle::NvIndex(nv_index_handle);
114+
let (data_size, nv_idx, auth_handle) = match self {
115+
NvOpenOptions::Index {
116+
nv_index_handle,
117+
auth_handle,
118+
} => {
119+
let nv_idx = TpmHandle::NvIndex(*nv_index_handle);
124120
let nv_idx = context
125121
.execute_without_session(|ctx| ctx.tr_from_tpm_public(nv_idx))?
126122
.into();
@@ -129,23 +125,25 @@ impl NvOpenOptions {
129125
.execute_without_session(|ctx| ctx.nv_read_public(nv_idx))
130126
.map(|(nvpub, _)| nvpub.data_size())?,
131127
nv_idx,
128+
auth_handle,
132129
)
133130
}
134-
Some(nv_public) => {
135-
if nv_public.nv_index() != nv_index_handle {
136-
return Err(Error::WrapperError(WrapperErrorKind::InconsistentParams));
137-
}
138-
let auth_handle = AuthHandle::from(auth_handle);
131+
NvOpenOptions::Public {
132+
nv_public,
133+
auth_handle,
134+
} => {
135+
let auth = AuthHandle::from(*auth_handle);
139136
(
140137
nv_public.data_size(),
141-
context.nv_define_space(auth_handle.try_into()?, None, nv_public.clone())?,
138+
context.nv_define_space(auth.try_into()?, None, nv_public.clone())?,
139+
auth_handle,
142140
)
143141
}
144142
};
145143

146144
Ok(NvReaderWriter {
147145
context,
148-
auth_handle,
146+
auth_handle: *auth_handle,
149147
buffer_size,
150148
nv_idx,
151149
data_size,

tss-esapi/tests/integration_tests/abstraction_tests/nv_tests.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ fn write() {
116116
.with_owner_write(true)
117117
.with_owner_read(true)
118118
.with_pp_read(true)
119-
.with_owner_read(true)
120119
.build()
121120
.expect("Failed to create owner nv index attributes");
122121
let owner_nv_public = NvPublicBuilder::new()
@@ -127,10 +126,12 @@ fn write() {
127126
.build()
128127
.unwrap();
129128

130-
let mut rw = nv::NvOpenOptions::new()
131-
.with_nv_public(Some(owner_nv_public))
132-
.open(&mut context, NvAuth::Owner, nv_index)
133-
.unwrap();
129+
let mut rw = nv::NvOpenOptions::Public {
130+
nv_public: owner_nv_public,
131+
auth_handle: NvAuth::Owner,
132+
}
133+
.open(&mut context)
134+
.unwrap();
134135

135136
let value = [1, 2, 3, 4, 5, 6, 7];
136137
rw.write_all(&value).unwrap();

0 commit comments

Comments
 (0)