Skip to content

Commit 2765af6

Browse files
authored
Merge pull request #304 from Jakuje/pkcs11-profile
Add support for Pkcs#11 Profile objects
2 parents 98f269a + 57b79fc commit 2765af6

File tree

2 files changed

+118
-6
lines changed

2 files changed

+118
-6
lines changed

cryptoki/src/object.rs

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ pub enum AttributeType {
9494
ObjectId,
9595
/// DER encoding of the attribute certificate's subject field
9696
Owner,
97+
/// Algorithm-specific parameter set
98+
ParameterSet,
9799
/// Prime number value of a key
98100
Prime,
99101
/// The prime `p` of an RSA private key
@@ -108,6 +110,10 @@ pub enum AttributeType {
108110
PublicExponent,
109111
/// DER-encoding of the SubjectPublicKeyInfo
110112
PublicKeyInfo,
113+
/// Profile ID
114+
ProfileId,
115+
/// Seed to derive private key
116+
Seed,
111117
/// Determines if the key is sensitive
112118
Sensitive,
113119
/// DER encoding of the certificate serial number
@@ -144,10 +150,6 @@ pub enum AttributeType {
144150
Wrap,
145151
/// Indicates that the key can only be wrapped with a wrapping key that has the Trusted attribute
146152
WrapWithTrusted,
147-
/// Seed to derive private key
148-
Seed,
149-
/// Algorithm-specific parameter set
150-
ParameterSet,
151153
}
152154

153155
impl AttributeType {
@@ -269,6 +271,7 @@ impl AttributeType {
269271
CKA_UNIQUE_ID => String::from(stringify!(CKA_UNIQUE_ID)),
270272
CKA_SEED => String::from(stringify!(CKA_SEED)),
271273
CKA_PARAMETER_SET => String::from(stringify!(CKA_PARAMETER_SET)),
274+
CKA_PROFILE_ID => String::from(stringify!(CKA_PROFILE_ID)),
272275
CKA_VENDOR_DEFINED..=CK_ULONG::MAX => {
273276
format!("{}_{}", stringify!(CKA_VENDOR_DEFINED), val)
274277
}
@@ -331,6 +334,7 @@ impl From<AttributeType> for CK_ATTRIBUTE_TYPE {
331334
AttributeType::Prime2 => CKA_PRIME_2,
332335
AttributeType::Private => CKA_PRIVATE,
333336
AttributeType::PrivateExponent => CKA_PRIVATE_EXPONENT,
337+
AttributeType::ProfileId => CKA_PROFILE_ID,
334338
AttributeType::PublicExponent => CKA_PUBLIC_EXPONENT,
335339
AttributeType::PublicKeyInfo => CKA_PUBLIC_KEY_INFO,
336340
AttributeType::Seed => CKA_SEED,
@@ -405,6 +409,7 @@ impl TryFrom<CK_ATTRIBUTE_TYPE> for AttributeType {
405409
CKA_PRIME_2 => Ok(AttributeType::Prime2),
406410
CKA_PRIVATE => Ok(AttributeType::Private),
407411
CKA_PRIVATE_EXPONENT => Ok(AttributeType::PrivateExponent),
412+
CKA_PROFILE_ID => Ok(AttributeType::ProfileId),
408413
CKA_PUBLIC_EXPONENT => Ok(AttributeType::PublicExponent),
409414
CKA_PUBLIC_KEY_INFO => Ok(AttributeType::PublicKeyInfo),
410415
CKA_SEED => Ok(AttributeType::Seed),
@@ -526,6 +531,8 @@ pub enum Attribute {
526531
Private(bool),
527532
/// The private exponent `d`
528533
PrivateExponent(Vec<u8>),
534+
/// The Profile ID
535+
ProfileId(ProfileIdType),
529536
/// Public exponent value of a key
530537
PublicExponent(Vec<u8>),
531538
/// DER-encoding of the SubjectPublicKeyInfo
@@ -618,6 +625,7 @@ impl Attribute {
618625
Attribute::Prime2(_) => AttributeType::Prime2,
619626
Attribute::Private(_) => AttributeType::Private,
620627
Attribute::PrivateExponent(_) => AttributeType::PrivateExponent,
628+
Attribute::ProfileId(_) => AttributeType::ProfileId,
621629
Attribute::PublicExponent(_) => AttributeType::PublicExponent,
622630
Attribute::PublicKeyInfo(_) => AttributeType::PublicKeyInfo,
623631
Attribute::Seed(_) => AttributeType::Seed,
@@ -698,6 +706,7 @@ impl Attribute {
698706
Attribute::Prime1(bytes) => bytes.len(),
699707
Attribute::Prime2(bytes) => bytes.len(),
700708
Attribute::PrivateExponent(bytes) => bytes.len(),
709+
Attribute::ProfileId(_) => size_of::<CK_PROFILE_ID>(),
701710
Attribute::PublicExponent(bytes) => bytes.len(),
702711
Attribute::PublicKeyInfo(bytes) => bytes.len(),
703712
Attribute::Seed(bytes) => bytes.len(),
@@ -792,6 +801,7 @@ impl Attribute {
792801
| Attribute::Id(bytes) => bytes.as_ptr() as *mut c_void,
793802
// Unique types
794803
Attribute::ParameterSet(val) => val as *const _ as *mut c_void,
804+
Attribute::ProfileId(val) => val as *const _ as *mut c_void,
795805
Attribute::CertificateType(certificate_type) => {
796806
certificate_type as *const _ as *mut c_void
797807
}
@@ -922,6 +932,9 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
922932
AttributeType::Value => Ok(Attribute::Value(val.to_vec())),
923933
AttributeType::Id => Ok(Attribute::Id(val.to_vec())),
924934
// Unique types
935+
AttributeType::ProfileId => Ok(Attribute::ProfileId(ProfileIdType {
936+
val: CK_ULONG::from_ne_bytes(val.try_into()?),
937+
})),
925938
AttributeType::ParameterSet => Ok(Attribute::ParameterSet(ParameterSetType {
926939
val: CK_ULONG::from_ne_bytes(val.try_into()?).into(),
927940
})),
@@ -1278,6 +1291,12 @@ impl ObjectClass {
12781291
pub const MECHANISM: ObjectClass = ObjectClass { val: CKO_MECHANISM };
12791292
/// An OTP key object
12801293
pub const OTP_KEY: ObjectClass = ObjectClass { val: CKO_OTP_KEY };
1294+
/// Profile object
1295+
pub const PROFILE: ObjectClass = ObjectClass { val: CKO_PROFILE };
1296+
/// Validation object
1297+
pub const VALIDATION: ObjectClass = ObjectClass {
1298+
val: CKO_VALIDATION,
1299+
};
12811300

12821301
pub(crate) fn stringify(class: CK_OBJECT_CLASS) -> String {
12831302
match class {
@@ -1290,6 +1309,8 @@ impl ObjectClass {
12901309
CKO_DOMAIN_PARAMETERS => String::from(stringify!(CKO_DOMAIN_PARAMETERS)),
12911310
CKO_MECHANISM => String::from(stringify!(CKO_MECHANISM)),
12921311
CKO_OTP_KEY => String::from(stringify!(CKO_OTP_KEY)),
1312+
CKO_PROFILE => String::from(stringify!(CKO_PROFILE)),
1313+
CKO_VALIDATION => String::from(stringify!(CKO_VALIDATION)),
12931314
_ => format!("unknown ({class:08x})"),
12941315
}
12951316
}
@@ -1329,6 +1350,8 @@ impl TryFrom<CK_OBJECT_CLASS> for ObjectClass {
13291350
CKO_DOMAIN_PARAMETERS => Ok(ObjectClass::DOMAIN_PARAMETERS),
13301351
CKO_MECHANISM => Ok(ObjectClass::MECHANISM),
13311352
CKO_OTP_KEY => Ok(ObjectClass::OTP_KEY),
1353+
CKO_PROFILE => Ok(ObjectClass::PROFILE),
1354+
CKO_VALIDATION => Ok(ObjectClass::VALIDATION),
13321355

13331356
_ => {
13341357
error!("Object class {} is not supported.", object_class);
@@ -1708,3 +1731,92 @@ impl TryFrom<CK_CERTIFICATE_TYPE> for CertificateType {
17081731
}
17091732
}
17101733
}
1734+
1735+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
1736+
#[repr(transparent)]
1737+
/// The PKCS#11 Profile ID
1738+
///
1739+
/// The profiles and their meaning is defined in the following document:
1740+
///
1741+
/// <https://docs.oasis-open.org/pkcs11/pkcs11-profiles/v3.1/os/pkcs11-profiles-v3.1-os.html>
1742+
pub struct ProfileIdType {
1743+
val: CK_PROFILE_ID,
1744+
}
1745+
1746+
impl ProfileIdType {
1747+
/// Baseline Provider
1748+
pub const BASELINE_PROFIDER: ProfileIdType = ProfileIdType {
1749+
val: CKP_BASELINE_PROVIDER,
1750+
};
1751+
/// Extended Provider
1752+
pub const EXTENDED_PROFIDER: ProfileIdType = ProfileIdType {
1753+
val: CKP_EXTENDED_PROVIDER,
1754+
};
1755+
/// Authentication Token Provider or Consumer
1756+
pub const AUTHENTICATION_TOKEN: ProfileIdType = ProfileIdType {
1757+
val: CKP_AUTHENTICATION_TOKEN,
1758+
};
1759+
/// Public Certificates Token Provider or Consumer
1760+
pub const PUBLIC_CERTIFICATES_TOKEN: ProfileIdType = ProfileIdType {
1761+
val: CKP_PUBLIC_CERTIFICATES_TOKEN,
1762+
};
1763+
/// Complete Provider
1764+
pub const COMPLETE_PROVIDER: ProfileIdType = ProfileIdType {
1765+
val: CKP_COMPLETE_PROVIDER,
1766+
};
1767+
/// HKDF TLS Token
1768+
pub const HKDF_TLS_TOKEN: ProfileIdType = ProfileIdType {
1769+
val: CKP_HKDF_TLS_TOKEN,
1770+
};
1771+
}
1772+
1773+
impl std::fmt::Display for ProfileIdType {
1774+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1775+
write!(
1776+
f,
1777+
"{}",
1778+
match self.val {
1779+
CKP_BASELINE_PROVIDER => stringify!(CKP_BASELINE_PROVIDER),
1780+
CKP_EXTENDED_PROVIDER => stringify!(CKP_EXTENDED_PROVIDER),
1781+
CKP_AUTHENTICATION_TOKEN => stringify!(CKP_AUTHENTICATION_TOKEN),
1782+
CKP_PUBLIC_CERTIFICATES_TOKEN => {
1783+
stringify!(CKP_PUBLIC_CERTIFICATES_TOKEN)
1784+
}
1785+
CKP_COMPLETE_PROVIDER => stringify!(CKP_COMPLETE_PROVIDER),
1786+
CKP_HKDF_TLS_TOKEN => stringify!(CKP_HKDF_TLS_TOKEN),
1787+
profile_id => return write!(f, "unknown ({profile_id:08x})"),
1788+
}
1789+
)
1790+
}
1791+
}
1792+
1793+
impl AsRef<CK_PROFILE_ID> for ProfileIdType {
1794+
fn as_ref(&self) -> &CK_PROFILE_ID {
1795+
&self.val
1796+
}
1797+
}
1798+
1799+
impl From<ProfileIdType> for CK_PROFILE_ID {
1800+
fn from(profile_id: ProfileIdType) -> Self {
1801+
*profile_id.as_ref()
1802+
}
1803+
}
1804+
1805+
impl TryFrom<CK_PROFILE_ID> for ProfileIdType {
1806+
type Error = Error;
1807+
1808+
fn try_from(profile_id: CK_PROFILE_ID) -> Result<Self> {
1809+
match profile_id {
1810+
CKP_BASELINE_PROVIDER => Ok(ProfileIdType::BASELINE_PROFIDER),
1811+
CKP_EXTENDED_PROVIDER => Ok(ProfileIdType::EXTENDED_PROFIDER),
1812+
CKP_AUTHENTICATION_TOKEN => Ok(ProfileIdType::AUTHENTICATION_TOKEN),
1813+
CKP_PUBLIC_CERTIFICATES_TOKEN => Ok(ProfileIdType::PUBLIC_CERTIFICATES_TOKEN),
1814+
CKP_COMPLETE_PROVIDER => Ok(ProfileIdType::COMPLETE_PROVIDER),
1815+
CKP_HKDF_TLS_TOKEN => Ok(ProfileIdType::HKDF_TLS_TOKEN),
1816+
_ => {
1817+
error!("Profile Id {} is not supported.", profile_id);
1818+
Err(Error::NotSupported)
1819+
}
1820+
}
1821+
}
1822+
}

cryptoki/src/session/object_management.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl Session {
224224
/// * [`ObjectHandleIterator`] for more information on how to use the iterator
225225
/// * [`Session::iter_objects_with_cache_size`] for a way to specify the cache size
226226
#[inline(always)]
227-
pub fn iter_objects(&self, template: &[Attribute]) -> Result<ObjectHandleIterator> {
227+
pub fn iter_objects(&self, template: &[Attribute]) -> Result<ObjectHandleIterator<'_>> {
228228
self.iter_objects_with_cache_size(template, MAX_OBJECT_COUNT)
229229
}
230230

@@ -248,7 +248,7 @@ impl Session {
248248
&self,
249249
template: &[Attribute],
250250
cache_size: NonZeroUsize,
251-
) -> Result<ObjectHandleIterator> {
251+
) -> Result<ObjectHandleIterator<'_>> {
252252
let template: Vec<CK_ATTRIBUTE> = template.iter().map(Into::into).collect();
253253
ObjectHandleIterator::new(self, template, cache_size)
254254
}

0 commit comments

Comments
 (0)