Skip to content
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

Info flags refactor #68

Merged
merged 36 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3241c1b
Add a missing flag value used by TokenInfo
vkkoskie Nov 11, 2021
5951c48
Add two missing MechanismInfo flags
vkkoskie Nov 11, 2021
59fc60d
Remove unused SlotInfo traits
vkkoskie Nov 6, 2021
0fcd611
Convert SlotInfo contents once
vkkoskie Nov 6, 2021
8e5db26
Replace SlotInfo::new() with From impl
vkkoskie Nov 6, 2021
937d904
Merge get_all_slots and get_all_slots_with_token
vkkoskie Nov 6, 2021
e4436e2
Fix race condition in get_slots
vkkoskie Nov 6, 2021
10928a1
Expose SlotInfo flags individually
vkkoskie Nov 6, 2021
26925ab
Privatize SlotFlags; remove usused methods
vkkoskie Nov 6, 2021
6e6a2e9
Reimplement generic flags, then use in SlotInfo
vkkoskie Nov 10, 2021
9956187
Update SlotInfo docs
vkkoskie Nov 10, 2021
a573088
Refactor TokenInfo Flags
vkkoskie Nov 11, 2021
54cb98c
Hide conversions from sys crate in public docs
vkkoskie Nov 11, 2021
bee02b9
Refactor MechanismInfo Flags
vkkoskie Nov 12, 2021
0ae3adb
Move info types
vkkoskie Nov 12, 2021
aab33b7
Tidy info/flag docs
vkkoskie Nov 12, 2021
5bb40c6
Add Version constructor
vkkoskie Nov 12, 2021
88134f0
Add unit tests for info+flags debug content
vkkoskie Nov 12, 2021
de46362
Remove Display trait from Flag types
vkkoskie Nov 12, 2021
1b7f520
Tidy imports
vkkoskie Nov 12, 2021
0650deb
Remove some stray comment errors
vkkoskie Nov 17, 2021
5dfddf8
Add UtcTime implementation
vkkoskie Nov 18, 2021
46a2691
Make UtcTime conversion fallible
vkkoskie Nov 19, 2021
261d934
Refactor SessionInfo
vkkoskie Dec 3, 2021
67e7589
Replace SessionFlags with bool to open session
vkkoskie Dec 3, 2021
e221cf6
Remove unused SessionFlags type
vkkoskie Dec 4, 2021
992bfbf
Refactor SessionState
vkkoskie Dec 4, 2021
d4917a8
Remove InitializeFlags
vkkoskie Dec 4, 2021
1cbad05
Replace Display for UtcTime with named function
vkkoskie Dec 4, 2021
7ffa711
Replace custom CkFlags impl with bitflags crate
vkkoskie Dec 4, 2021
6dacb62
Refactor general Info struct
vkkoskie Dec 4, 2021
f18c73e
Replace nested Options with Limit newtype
vkkoskie Jan 13, 2022
40810ee
Add comment to explain test condition
vkkoskie Jan 13, 2022
7a0ad30
Move Option variants to single scope
vkkoskie Jan 14, 2022
aae8de0
Reduce visibility of Version constructor
vkkoskie Jan 14, 2022
b15eecd
Minor fix/enhance of a few comments
vkkoskie Jan 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cryptoki-sys/pkcs11.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ struct ck_token_info
#define CKF_SO_PIN_FINAL_TRY (1UL << 21)
#define CKF_SO_PIN_LOCKED (1UL << 22)
#define CKF_SO_PIN_TO_BE_CHANGED (1UL << 23)
#define CKF_ERROR_STATE (1UL << 24)

#define CK_UNAVAILABLE_INFORMATION ((unsigned long)-1L)
#define CK_EFFECTIVELY_INFINITE (0UL)
Expand Down Expand Up @@ -1044,6 +1045,8 @@ struct ck_aes_cbc_encrypt_data_params {
#define CKF_EXTENSION ((unsigned long) (1UL << 31))

#define CKF_EC_F_P (1UL << 20)
#define CKF_EC_F_2M (1UL << 21)
#define CKF_EC_ECPARAMETERS (1UL << 22)
#define CKF_EC_NAMEDCURVE (1UL << 23)
#define CKF_EC_UNCOMPRESS (1UL << 24)
#define CKF_EC_COMPRESS (1UL << 25)
Expand Down
3 changes: 3 additions & 0 deletions cryptoki-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub const CKF_SO_PIN_COUNT_LOW: CK_FLAGS = 0x00100000;
pub const CKF_SO_PIN_FINAL_TRY: CK_FLAGS = 0x00200000;
pub const CKF_SO_PIN_LOCKED: CK_FLAGS = 0x00400000;
pub const CKF_SO_PIN_TO_BE_CHANGED: CK_FLAGS = 0x00800000;
pub const CKF_ERROR_STATE: CK_FLAGS = 0x01000000;
pub const CK_UNAVAILABLE_INFORMATION: CK_ULONG = !0;
pub const CK_EFFECTIVELY_INFINITE: CK_ULONG = 0;
pub const CK_INVALID_HANDLE: CK_ULONG = 0;
Expand Down Expand Up @@ -689,6 +690,8 @@ pub const CKF_UNWRAP: CK_FLAGS = 0x00040000;
pub const CKF_DERIVE: CK_FLAGS = 0x00080000;
pub const CKF_EXTENSION: CK_FLAGS = 0x80000000;
pub const CKF_EC_F_P: CK_FLAGS = 0x00100000;
pub const CKF_EC_F_2M: CK_FLAGS = 0x00200000;
pub const CKF_EC_ECPARAMETERS: CK_FLAGS = 0x00400000;
pub const CKF_EC_NAMEDCURVE: CK_FLAGS = 0x00800000;
pub const CKF_EC_UNCOMPRESS: CK_FLAGS = 0x01000000;
pub const CKF_EC_COMPRESS: CK_FLAGS = 0x02000000;
Expand Down
1 change: 1 addition & 0 deletions cryptoki/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = "https://github.com/parallaxsecond/rust-cryptoki"
documentation = "https://docs.rs/crate/cryptoki"

[dependencies]
bitflags = "1.3"
libloading = "0.7.0"
log = "0.4.14"
derivative = "2.2.0"
Expand Down
99 changes: 0 additions & 99 deletions cryptoki/src/context/flags.rs

This file was deleted.

3 changes: 2 additions & 1 deletion cryptoki/src/context/general_purpose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::context::{CInitializeArgs, Info, Pkcs11};
use crate::error::{Result, Rv};
use cryptoki_sys::{CK_C_INITIALIZE_ARGS, CK_INFO};
use std::convert::TryFrom;

// See public docs on stub in parent mod.rs
#[inline(always)]
Expand All @@ -26,6 +27,6 @@ pub(super) fn get_library_info(ctx: &Pkcs11) -> Result<Info> {
let mut info = CK_INFO::default();
unsafe {
Rv::from(get_pkcs11!(ctx, C_GetInfo)(&mut info)).into_result()?;
Ok(Info::new(info))
Info::try_from(info)
}
}
73 changes: 39 additions & 34 deletions cryptoki/src/context/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,63 @@
// SPDX-License-Identifier: Apache-2.0
//! PKCS11 library information

use crate::error::{Error, Result};
use crate::string_from_blank_padded;
use crate::types::Version;
use cryptoki_sys::*;
use std::ops::Deref;
use std::convert::TryFrom;

#[derive(Debug, Clone, Copy)]
/// Type identifying the PKCS#11 library information
#[derive(Debug, Clone)]
/// General information about the Cryptoki (PKCS#11 library)
pub struct Info {
val: CK_INFO,
cryptoki_version: Version,
manufacturer_id: String,
// flags
library_description: String,
library_version: Version,
}

impl Info {
pub(crate) fn new(val: CK_INFO) -> Self {
Self { val }
}

/// Returns the version of Cryptoki that the library is compatible with
/// Returns the version of Cryptoki interface for compatibility with future
/// revisions
pub fn cryptoki_version(&self) -> Version {
self.val.cryptokiVersion.into()
self.cryptoki_version
}

/// Returns the flags of the library (should be zero!)
pub fn flags(&self) -> CK_FLAGS {
self.val.flags
/// ID of the Cryptoki library manufacturer
///
/// **[Conformance](crate#conformance-notes):**
/// This string is maximally 32 bytes (*not* chars) as UTF-8
pub fn manufacturer_id(&self) -> &str {
&self.manufacturer_id
}

/// Returns the description of the library
pub fn library_description(&self) -> String {
string_from_blank_padded(&self.val.libraryDescription)
/// Description of the library
///
/// **[Conformance](crate#conformance-notes):**
/// This string is maximally 32 bytes (*not* chars) as UTF-8
pub fn library_description(&self) -> &str {
&self.library_description
}

/// Returns the version of the library
/// Cryptoki library version number
pub fn library_version(&self) -> Version {
self.val.libraryVersion.into()
}

/// Returns the manufacturer of the library
pub fn manufacturer_id(&self) -> String {
string_from_blank_padded(&self.val.manufacturerID)
}
}

impl Deref for Info {
type Target = CK_INFO;

fn deref(&self) -> &Self::Target {
&self.val
self.library_version
}
}

impl From<Info> for CK_INFO {
fn from(info: Info) -> Self {
*info
#[doc(hidden)]
impl TryFrom<CK_INFO> for Info {
type Error = Error;
fn try_from(val: CK_INFO) -> Result<Self> {
if val.flags != 0 {
return Err(Error::InvalidValue);
}
Ok(Self {
cryptoki_version: val.cryptokiVersion.into(),
manufacturer_id: string_from_blank_padded(&val.manufacturerID),
library_description: string_from_blank_padded(&val.libraryDescription),
library_version: val.libraryVersion.into(),
})
}
}
9 changes: 5 additions & 4 deletions cryptoki/src/context/locking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
//! Locking related type

use super::InitializeFlags;
use cryptoki_sys::{CKF_OS_LOCKING_OK, CK_FLAGS};

use std::ptr;

/// Argument for the initialize function
Expand All @@ -16,12 +17,12 @@ pub enum CInitializeArgs {

impl From<CInitializeArgs> for cryptoki_sys::CK_C_INITIALIZE_ARGS {
fn from(c_initialize_args: CInitializeArgs) -> Self {
let mut flags = InitializeFlags::default();
let mut flags = CK_FLAGS::default();
match c_initialize_args {
CInitializeArgs::OsThreads => {
let _ = flags.set_os_locking_ok(true);
flags |= CKF_OS_LOCKING_OK;
Self {
flags: flags.into(),
flags,
CreateMutex: None,
DestroyMutex: None,
LockMutex: None,
Expand Down
13 changes: 6 additions & 7 deletions cryptoki/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ macro_rules! get_pkcs11 {
};
}

mod flags;
mod general_purpose;
mod info;
mod locking;
mod session_management;
mod slot_token_management;

pub use flags::*;
use cryptoki_sys::{CK_FALSE, CK_TRUE};
pub use info::*;
pub use locking::*;

use crate::error::{Error, Result, Rv};
use crate::mechanism::{MechanismInfo, MechanismType};
use crate::session::{Session, SessionFlags};
use crate::session::Session;
use crate::slot::{Slot, SlotInfo, TokenInfo};

use derivative::Derivative;
Expand Down Expand Up @@ -117,7 +116,7 @@ impl Pkcs11 {

/// Get all slots available with a token
pub fn get_slots_with_token(&self) -> Result<Vec<Slot>> {
slot_token_management::get_slots_with_token(self)
slot_token_management::get_slots(self, CK_TRUE)
}

/// Get all slots available with a token
Expand All @@ -127,7 +126,7 @@ impl Pkcs11 {

/// Get all slots
pub fn get_all_slots(&self) -> Result<Vec<Slot>> {
slot_token_management::get_all_slots(self)
slot_token_management::get_slots(self, CK_FALSE)
}

/// Initialize a token
Expand Down Expand Up @@ -158,7 +157,7 @@ impl Pkcs11 {
}

/// Open a new session with no callback set
pub fn open_session_no_callback(&self, slot_id: Slot, flags: SessionFlags) -> Result<Session> {
session_management::open_session_no_callback(self, slot_id, flags)
pub fn open_session_no_callback(&self, slot_id: Slot, read_write: bool) -> Result<Session> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I'm not really too much into having boolean parameters since it's not obvious what open_session_no_callback(..., true) does (an enum with READ_WRITE or READ_ONLY would be more... readable) but on the other hand it's commonly used and the IDE can offer hints.

Or even export two functions: open_readonly_session_no_callback or open_readwrite_session_no_callback...

Btw very nice to see you providing explicit reasoning in the commit message for SessionFlags refactor: 67e7589 👏 thanks!

session_management::open_session_no_callback(self, slot_id, read_write)
}
}
14 changes: 10 additions & 4 deletions cryptoki/src/context/session_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@
// SPDX-License-Identifier: Apache-2.0
//! Session management functions

use cryptoki_sys::{CKF_RW_SESSION, CKF_SERIAL_SESSION};

use crate::context::Pkcs11;
use crate::error::{Result, Rv};
use crate::session::{Session, SessionFlags};
use crate::session::Session;
use crate::slot::Slot;
use std::convert::TryInto;

// See public docs on stub in parent mod.rs
#[inline(always)]
pub(super) fn open_session_no_callback(
ctx: &Pkcs11,
slot_id: Slot,
flags: SessionFlags,
read_write: bool,
) -> Result<Session> {
let mut session_handle = 0;

let flags = if read_write {
CKF_SERIAL_SESSION | CKF_RW_SESSION
} else {
CKF_SERIAL_SESSION
};
unsafe {
Rv::from(get_pkcs11!(ctx, C_OpenSession)(
slot_id.try_into()?,
flags.into(),
flags,
// TODO: abstract those types or create new functions for callbacks
std::ptr::null_mut(),
None,
Expand Down
Loading