@@ -12,7 +12,8 @@ use std::convert::TryInto;
12
12
use std:: num:: NonZeroUsize ;
13
13
14
14
// Search 10 elements at a time
15
- const MAX_OBJECT_COUNT : usize = 10 ;
15
+ // Safety: the value provided (10) must be non-zero
16
+ const MAX_OBJECT_COUNT : NonZeroUsize = unsafe { NonZeroUsize :: new_unchecked ( 10 ) } ;
16
17
17
18
/// Iterator over object handles, in an active session.
18
19
///
@@ -37,16 +38,16 @@ const MAX_OBJECT_COUNT: usize = 10;
37
38
/// use std::env;
38
39
///
39
40
/// # fn main() -> testresult::TestResult {
40
- /// let pkcs11 = Pkcs11::new(
41
- /// env::var("PKCS11_SOFTHSM2_MODULE")
42
- /// .unwrap_or_else(|_| "/usr/local/lib/libsofthsm2.so".to_string()),
43
- /// )?;
44
- ///
45
- /// pkcs11.initialize(CInitializeArgs::OsThreads)?;
46
- /// let slot = pkcs11.get_slots_with_token()?.remove(0);
47
- ///
48
- /// let session = pkcs11.open_ro_session(slot).unwrap();
49
- /// session.login(UserType::User, Some(&AuthPin::new("fedcba".into())))?;
41
+ /// # let pkcs11 = Pkcs11::new(
42
+ /// # env::var("PKCS11_SOFTHSM2_MODULE")
43
+ /// # .unwrap_or_else(|_| "/usr/local/lib/libsofthsm2.so".to_string()),
44
+ /// # )?;
45
+ /// #
46
+ /// # pkcs11.initialize(CInitializeArgs::OsThreads)?;
47
+ /// # let slot = pkcs11.get_slots_with_token()?.remove(0);
48
+ /// #
49
+ /// # let session = pkcs11.open_ro_session(slot).unwrap();
50
+ /// # session.login(UserType::User, Some(&AuthPin::new("fedcba".into())))?;
50
51
///
51
52
/// let token_object = vec![Attribute::Token(true)];
52
53
/// let wanted_attr = vec![AttributeType::Label];
@@ -84,6 +85,28 @@ pub struct ObjectHandleIterator<'a> {
84
85
}
85
86
86
87
impl < ' a > ObjectHandleIterator < ' a > {
88
+ /// Create a new iterator over object handles.
89
+ ///
90
+ /// # Arguments
91
+ ///
92
+ /// * `session` - The session to iterate over
93
+ /// * `template` - The template to match objects against
94
+ /// * `cache_size` - The number of objects to cache (type is [`NonZeroUsize`])
95
+ ///
96
+ /// # Returns
97
+ ///
98
+ /// This function will return a [`Result<ObjectHandleIterator>`] that can be used to iterate over the objects
99
+ /// matching the template. The cache size corresponds to the size of the array provided to `C_FindObjects()`.
100
+ ///
101
+ /// # Errors
102
+ ///
103
+ /// This function will return an error if the call to `C_FindObjectsInit` fails.
104
+ ///
105
+ /// # Note
106
+ ///
107
+ /// The iterator `new()` method will call `C_FindObjectsInit`. It means that until the iterator is dropped,
108
+ /// creating another iterator will result in an error (typically `RvError::OperationActive` ).
109
+ ///
87
110
fn new (
88
111
session : & ' a Session ,
89
112
mut template : Vec < CK_ATTRIBUTE > ,
@@ -171,11 +194,15 @@ impl<'a> Iterator for ObjectHandleIterator<'a> {
171
194
172
195
impl Drop for ObjectHandleIterator < ' _ > {
173
196
fn drop ( & mut self ) {
174
- // bark but pass if C_FindObjectsFinal() is not implemented
175
197
if let Some ( f) = get_pkcs11_func ! ( self . session. client( ) , C_FindObjectsFinal ) {
198
+ // swallow the return value, as we can't do anything about it,
199
+ // but log the error
200
+ if let Rv :: Error ( error) = Rv :: from ( unsafe { f ( self . session . handle ( ) ) } ) {
201
+ log:: error!( "C_FindObjectsFinal() failed with error: {:?}" , error) ;
202
+ }
203
+ } else {
204
+ // bark but pass if C_FindObjectsFinal() is not implemented
176
205
log:: error!( "C_FindObjectsFinal() is not implemented on this library" ) ;
177
- // swallow the return value, as we can't do anything about it
178
- let _ = unsafe { f ( self . session . handle ( ) ) } ;
179
206
}
180
207
}
181
208
}
@@ -198,7 +225,7 @@ impl Session {
198
225
/// * [`Session::iter_objects_with_cache_size`] for a way to specify the cache size
199
226
#[ inline( always) ]
200
227
pub fn iter_objects ( & self , template : & [ Attribute ] ) -> Result < ObjectHandleIterator > {
201
- self . iter_objects_with_cache_size ( template, NonZeroUsize :: new ( MAX_OBJECT_COUNT ) . unwrap ( ) )
228
+ self . iter_objects_with_cache_size ( template, MAX_OBJECT_COUNT )
202
229
}
203
230
204
231
/// Iterate over session objects matching a template, with cache size
@@ -279,8 +306,7 @@ impl Session {
279
306
///
280
307
#[ inline( always) ]
281
308
pub fn find_objects ( & self , template : & [ Attribute ] ) -> Result < Vec < ObjectHandle > > {
282
- self . iter_objects ( template) ?
283
- . collect :: < Result < Vec < ObjectHandle > > > ( )
309
+ self . iter_objects ( template) ?. collect ( )
284
310
}
285
311
286
312
/// Create a new object
0 commit comments