-
Notifications
You must be signed in to change notification settings - Fork 81
Incremental object search #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
jhagborgftx
wants to merge
4
commits into
parallaxsecond:main
from
jhagborgftx:incremental-object-search
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,62 +6,107 @@ use crate::error::{Result, Rv, RvError}; | |
use crate::object::{Attribute, AttributeInfo, AttributeType, ObjectHandle}; | ||
use crate::session::Session; | ||
use cryptoki_sys::*; | ||
use log::error; | ||
use std::collections::HashMap; | ||
use std::convert::TryInto; | ||
|
||
// Search 10 elements at a time | ||
const MAX_OBJECT_COUNT: usize = 10; | ||
|
||
// See public docs on stub in parent mod.rs | ||
#[inline(always)] | ||
pub(super) fn find_objects(session: &Session, template: &[Attribute]) -> Result<Vec<ObjectHandle>> { | ||
let mut template: Vec<CK_ATTRIBUTE> = template.iter().map(|attr| attr.into()).collect(); | ||
|
||
unsafe { | ||
Rv::from(get_pkcs11!(session.client(), C_FindObjectsInit)( | ||
session.handle(), | ||
template.as_mut_ptr(), | ||
template.len().try_into()?, | ||
)) | ||
.into_result()?; | ||
} | ||
|
||
let mut object_handles = [0; MAX_OBJECT_COUNT]; | ||
let mut object_count = 0; | ||
let mut objects = Vec::new(); | ||
/// Represents an ongoing object search | ||
/// | ||
/// See the documentation for [Session::find_objects_init]. | ||
#[derive(Debug)] | ||
pub struct FindObjects<'a> { | ||
session: &'a mut Session, | ||
} | ||
|
||
unsafe { | ||
Rv::from(get_pkcs11!(session.client(), C_FindObjects)( | ||
session.handle(), | ||
object_handles.as_mut_ptr() as CK_OBJECT_HANDLE_PTR, | ||
MAX_OBJECT_COUNT.try_into()?, | ||
&mut object_count, | ||
)) | ||
.into_result()?; | ||
} | ||
impl<'a> FindObjects<'a> { | ||
/// Continue an ongoing object search | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `max_objects` - The maximum number of objects to return | ||
/// | ||
/// # Returns | ||
/// | ||
/// This function returns up to `max_objects` objects. If there are no remaining | ||
/// objects, or `max_objects` is 0, then it returns an empty vector. | ||
pub fn find_next(&mut self, max_objects: usize) -> Result<Vec<ObjectHandle>> { | ||
if max_objects == 0 { | ||
return Ok(vec![]); | ||
} | ||
|
||
while object_count > 0 { | ||
objects.extend_from_slice(&object_handles[..object_count.try_into()?]); | ||
let mut object_handles = Vec::with_capacity(max_objects); | ||
let mut object_count = 0; | ||
|
||
unsafe { | ||
Rv::from(get_pkcs11!(session.client(), C_FindObjects)( | ||
session.handle(), | ||
object_handles.as_mut_ptr() as CK_OBJECT_HANDLE_PTR, | ||
MAX_OBJECT_COUNT.try_into()?, | ||
Rv::from(get_pkcs11!(self.session.client(), C_FindObjects)( | ||
self.session.handle(), | ||
object_handles.as_mut_ptr(), | ||
max_objects.try_into()?, | ||
&mut object_count, | ||
)) | ||
.into_result()?; | ||
object_handles.set_len(object_count.try_into()?) | ||
} | ||
|
||
Ok(object_handles.into_iter().map(ObjectHandle::new).collect()) | ||
} | ||
|
||
/// Get the session associated to the search | ||
pub fn session(&self) -> &Session { | ||
self.session | ||
} | ||
} | ||
|
||
impl<'a> Drop for FindObjects<'a> { | ||
fn drop(&mut self) { | ||
if let Err(e) = find_objects_final_private(self.session) { | ||
error!("Failed to terminate object search: {}", e); | ||
} | ||
} | ||
} | ||
|
||
fn find_objects_final_private(session: &Session) -> Result<()> { | ||
unsafe { | ||
Rv::from(get_pkcs11!(session.client(), C_FindObjectsFinal)( | ||
session.handle(), | ||
)) | ||
.into_result() | ||
} | ||
} | ||
|
||
// See public docs on stub in parent mod.rs | ||
#[inline(always)] | ||
pub(super) fn find_objects_init<'a>( | ||
session: &'a mut Session, | ||
template: &[Attribute], | ||
) -> Result<FindObjects<'a>> { | ||
let mut template: Vec<CK_ATTRIBUTE> = template.iter().map(|attr| attr.into()).collect(); | ||
unsafe { | ||
Rv::from(get_pkcs11!(session.client(), C_FindObjectsInit)( | ||
session.handle(), | ||
template.as_mut_ptr(), | ||
template.len().try_into()?, | ||
)) | ||
.into_result()?; | ||
} | ||
Ok(FindObjects { session }) | ||
} | ||
|
||
let objects = objects.into_iter().map(ObjectHandle::new).collect(); | ||
// Search 10 elements at a time | ||
const MAX_OBJECT_COUNT: usize = 10; | ||
Comment on lines
+95
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason for this choice? Just curious |
||
|
||
// See public docs on stub in parent mod.rs | ||
#[inline(always)] | ||
pub(super) fn find_objects( | ||
session: &mut Session, | ||
template: &[Attribute], | ||
) -> Result<Vec<ObjectHandle>> { | ||
let mut search = session.find_objects_init(template)?; | ||
let mut objects = Vec::new(); | ||
|
||
while let ref new_objects @ [_, ..] = search.find_next(MAX_OBJECT_COUNT)?[..] { | ||
objects.extend_from_slice(new_objects) | ||
} | ||
|
||
Ok(objects) | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent, thanks for adding an example.