Skip to content

Commit 6dacb62

Browse files
committed
Refactor general Info struct
In particular, it * removes a lot of unnecessary interaction with ffi types, * removes the flags from the public API, * flattens the contents of CK_INFO into the struct, * moves individual field conversions out of public getters into a one-time, struct-level conversion, and * makes conversion from CK_INFO fallible. Signed-off-by: Keith Koskie <[email protected]>
1 parent 7ffa711 commit 6dacb62

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

cryptoki/src/context/general_purpose.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::context::{CInitializeArgs, Info, Pkcs11};
66
use crate::error::{Result, Rv};
77
use cryptoki_sys::{CK_C_INITIALIZE_ARGS, CK_INFO};
8+
use std::convert::TryFrom;
89

910
// See public docs on stub in parent mod.rs
1011
#[inline(always)]
@@ -26,6 +27,6 @@ pub(super) fn get_library_info(ctx: &Pkcs11) -> Result<Info> {
2627
let mut info = CK_INFO::default();
2728
unsafe {
2829
Rv::from(get_pkcs11!(ctx, C_GetInfo)(&mut info)).into_result()?;
29-
Ok(Info::new(info))
30+
Info::try_from(info)
3031
}
3132
}

cryptoki/src/context/info.rs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,63 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//! PKCS11 library information
44
5+
use crate::error::{Error, Result};
56
use crate::string_from_blank_padded;
67
use crate::types::Version;
78
use cryptoki_sys::*;
8-
use std::ops::Deref;
9+
use std::convert::TryFrom;
910

10-
#[derive(Debug, Clone, Copy)]
11-
/// Type identifying the PKCS#11 library information
11+
#[derive(Debug, Clone)]
12+
/// General information about the Cryptoki (PKCS#11 library)
1213
pub struct Info {
13-
val: CK_INFO,
14+
cryptoki_version: Version,
15+
manufacturer_id: String,
16+
// flags
17+
library_description: String,
18+
library_version: Version,
1419
}
1520

1621
impl Info {
17-
pub(crate) fn new(val: CK_INFO) -> Self {
18-
Self { val }
19-
}
20-
21-
/// Returns the version of Cryptoki that the library is compatible with
22+
/// Returns the version of Cryptoki interface for compatibility with future
23+
/// revisions
2224
pub fn cryptoki_version(&self) -> Version {
23-
self.val.cryptokiVersion.into()
25+
self.cryptoki_version
2426
}
2527

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

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

36-
/// Returns the version of the library
44+
/// Cryptoki library version number
3745
pub fn library_version(&self) -> Version {
38-
self.val.libraryVersion.into()
39-
}
40-
41-
/// Returns the manufacturer of the library
42-
pub fn manufacturer_id(&self) -> String {
43-
string_from_blank_padded(&self.val.manufacturerID)
44-
}
45-
}
46-
47-
impl Deref for Info {
48-
type Target = CK_INFO;
49-
50-
fn deref(&self) -> &Self::Target {
51-
&self.val
46+
self.library_version
5247
}
5348
}
5449

55-
impl From<Info> for CK_INFO {
56-
fn from(info: Info) -> Self {
57-
*info
50+
#[doc(hidden)]
51+
impl TryFrom<CK_INFO> for Info {
52+
type Error = Error;
53+
fn try_from(val: CK_INFO) -> Result<Self> {
54+
if val.flags != 0 {
55+
return Err(Error::InvalidValue);
56+
}
57+
Ok(Self {
58+
cryptoki_version: val.cryptokiVersion.into(),
59+
manufacturer_id: string_from_blank_padded(&val.manufacturerID),
60+
library_description: string_from_blank_padded(&val.libraryDescription),
61+
library_version: val.libraryVersion.into(),
62+
})
5863
}
5964
}

0 commit comments

Comments
 (0)