|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | //! PKCS11 library information |
4 | 4 |
|
| 5 | +use crate::error::{Error, Result}; |
5 | 6 | use crate::string_from_blank_padded; |
6 | 7 | use crate::types::Version; |
7 | 8 | use cryptoki_sys::*; |
8 | | -use std::ops::Deref; |
| 9 | +use std::convert::TryFrom; |
9 | 10 |
|
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) |
12 | 13 | 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, |
14 | 19 | } |
15 | 20 |
|
16 | 21 | 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 |
22 | 24 | pub fn cryptoki_version(&self) -> Version { |
23 | | - self.val.cryptokiVersion.into() |
| 25 | + self.cryptoki_version |
24 | 26 | } |
25 | 27 |
|
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 |
29 | 34 | } |
30 | 35 |
|
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 |
34 | 42 | } |
35 | 43 |
|
36 | | - /// Returns the version of the library |
| 44 | + /// Cryptoki library version number |
37 | 45 | 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 |
52 | 47 | } |
53 | 48 | } |
54 | 49 |
|
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 | + }) |
58 | 63 | } |
59 | 64 | } |
0 commit comments