@@ -20,7 +20,11 @@ pub fn read_full(
20
20
auth_handle : NvAuth ,
21
21
nv_index_handle : NvIndexTpmHandle ,
22
22
) -> 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) ?;
24
28
let mut result = Vec :: with_capacity ( rw. size ( ) ) ;
25
29
26
30
let _ = rw. read_to_end ( & mut result) . map_err ( |e| {
@@ -80,47 +84,39 @@ pub fn list(context: &mut Context) -> Result<Vec<(NvPublic, Name)>> {
80
84
}
81
85
82
86
/// 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
+ } ,
89
97
}
90
98
91
99
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
-
108
100
/// Opens a non-volatile storage index using the options specified by `self`
109
101
///
110
102
/// 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 > > {
117
104
let buffer_size = context
118
105
. 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 ) ;
120
113
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) ;
124
120
let nv_idx = context
125
121
. execute_without_session ( |ctx| ctx. tr_from_tpm_public ( nv_idx) ) ?
126
122
. into ( ) ;
@@ -129,23 +125,25 @@ impl NvOpenOptions {
129
125
. execute_without_session ( |ctx| ctx. nv_read_public ( nv_idx) )
130
126
. map ( |( nvpub, _) | nvpub. data_size ( ) ) ?,
131
127
nv_idx,
128
+ auth_handle,
132
129
)
133
130
}
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) ;
139
136
(
140
137
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,
142
140
)
143
141
}
144
142
} ;
145
143
146
144
Ok ( NvReaderWriter {
147
145
context,
148
- auth_handle,
146
+ auth_handle : * auth_handle ,
149
147
buffer_size,
150
148
nv_idx,
151
149
data_size,
0 commit comments