Skip to content

Commit 4d71ade

Browse files
authored
Merge pull request #271 from Jakuje/unique-id
Add UniqueId Attribute
2 parents 19bb7b9 + b02dcbd commit 4d71ade

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

cryptoki/src/object.rs

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ pub enum AttributeType {
122122
Token,
123123
/// Determines if the object is trusted
124124
Trusted,
125+
/// Unique Object Id
126+
UniqueId,
125127
/// Determines if a key supports unwrapping
126128
Unwrap,
127129
/// Gives the URL where the complete certificate can be obtained
@@ -258,6 +260,7 @@ impl AttributeType {
258260
CKA_UNWRAP_TEMPLATE => String::from(stringify!(CKA_UNWRAP_TEMPLATE)),
259261
CKA_DERIVE_TEMPLATE => String::from(stringify!(CKA_DERIVE_TEMPLATE)),
260262
CKA_ALLOWED_MECHANISMS => String::from(stringify!(CKA_ALLOWED_MECHANISMS)),
263+
CKA_UNIQUE_ID => String::from(stringify!(CKA_UNIQUE_ID)),
261264
CKA_VENDOR_DEFINED..=MAX_CU_ULONG => {
262265
format!("{}_{}", stringify!(CKA_VENDOR_DEFINED), val)
263266
}
@@ -327,6 +330,7 @@ impl From<AttributeType> for CK_ATTRIBUTE_TYPE {
327330
AttributeType::Subject => CKA_SUBJECT,
328331
AttributeType::Token => CKA_TOKEN,
329332
AttributeType::Trusted => CKA_TRUSTED,
333+
AttributeType::UniqueId => CKA_UNIQUE_ID,
330334
AttributeType::Unwrap => CKA_UNWRAP,
331335
AttributeType::Url => CKA_URL,
332336
AttributeType::Value => CKA_VALUE,
@@ -396,6 +400,7 @@ impl TryFrom<CK_ATTRIBUTE_TYPE> for AttributeType {
396400
CKA_SUBJECT => Ok(AttributeType::Subject),
397401
CKA_TOKEN => Ok(AttributeType::Token),
398402
CKA_TRUSTED => Ok(AttributeType::Trusted),
403+
CKA_UNIQUE_ID => Ok(AttributeType::UniqueId),
399404
CKA_UNWRAP => Ok(AttributeType::Unwrap),
400405
CKA_URL => Ok(AttributeType::Url),
401406
CKA_VALUE => Ok(AttributeType::Value),
@@ -519,6 +524,8 @@ pub enum Attribute {
519524
Token(bool),
520525
/// Determines if an object is trusted
521526
Trusted(bool),
527+
/// Unique Object Id
528+
UniqueId(Vec<u8>),
522529
/// Determines if a key supports unwrapping
523530
Unwrap(bool),
524531
/// Gives the URL where the complete certificate can ber obtained
@@ -594,6 +601,7 @@ impl Attribute {
594601
Attribute::Subject(_) => AttributeType::Subject,
595602
Attribute::Token(_) => AttributeType::Token,
596603
Attribute::Trusted(_) => AttributeType::Trusted,
604+
Attribute::UniqueId(_) => AttributeType::UniqueId,
597605
Attribute::Unwrap(_) => AttributeType::Unwrap,
598606
Attribute::Url(_) => AttributeType::Url,
599607
Attribute::Value(_) => AttributeType::Value,
@@ -663,6 +671,7 @@ impl Attribute {
663671
Attribute::PublicKeyInfo(bytes) => bytes.len(),
664672
Attribute::SerialNumber(bytes) => bytes.len(),
665673
Attribute::Subject(bytes) => bytes.len(),
674+
Attribute::UniqueId(bytes) => bytes.len(),
666675
Attribute::Value(bytes) => bytes.len(),
667676
Attribute::ValueLen(_) => size_of::<CK_ULONG>(),
668677
Attribute::EndDate(_) | Attribute::StartDate(_) => size_of::<CK_DATE>(),
@@ -741,6 +750,7 @@ impl Attribute {
741750
| Attribute::Owner(bytes)
742751
| Attribute::SerialNumber(bytes)
743752
| Attribute::Subject(bytes)
753+
| Attribute::UniqueId(bytes)
744754
| Attribute::Url(bytes)
745755
| Attribute::Value(bytes)
746756
| Attribute::VendorDefined((_, bytes))
@@ -868,6 +878,7 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
868878
AttributeType::Owner => Ok(Attribute::Owner(val.to_vec())),
869879
AttributeType::SerialNumber => Ok(Attribute::SerialNumber(val.to_vec())),
870880
AttributeType::Subject => Ok(Attribute::Subject(val.to_vec())),
881+
AttributeType::UniqueId => Ok(Attribute::UniqueId(val.to_vec())),
871882
AttributeType::Url => Ok(Attribute::Url(val.to_vec())),
872883
AttributeType::Value => Ok(Attribute::Value(val.to_vec())),
873884
AttributeType::Id => Ok(Attribute::Id(val.to_vec())),

cryptoki/src/session/object_management.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const MAX_OBJECT_COUNT: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10)
5757
///
5858
/// let attributes = session.get_attributes(obj, &wanted_attr)?;
5959
///
60-
/// match attributes.get(0) {
60+
/// match attributes.first() {
6161
/// Some(Attribute::Label(l)) => {
6262
/// println!(
6363
/// "token object #{}: handle {}, label {}",
@@ -413,7 +413,7 @@ impl Session {
413413
/// session.login(UserType::User, Some(&AuthPin::new("fedcba".into())));
414414
///
415415
/// let empty_attrib= vec![];
416-
/// if let Some(object) = session.find_objects(&empty_attrib).unwrap().get(0) {
416+
/// if let Some(object) = session.find_objects(&empty_attrib).unwrap().first() {
417417
/// let attribute_types = vec![
418418
/// AttributeType::Token,
419419
/// AttributeType::Private,

cryptoki/tests/basic.rs

+79
Original file line numberDiff line numberDiff line change
@@ -2476,3 +2476,82 @@ fn aes_cmac_verify_impl(key: [u8; 16], message: &[u8], expected_mac: [u8; 16]) -
24762476
session.verify(&Mechanism::AesCMac, key, message, &expected_mac)?;
24772477
Ok(())
24782478
}
2479+
2480+
/// AES-CMAC test vectors from RFC 4493
2481+
#[test]
2482+
#[serial]
2483+
fn unique_id() -> TestResult {
2484+
let (pkcs11, slot) = init_pins();
2485+
let session = pkcs11.open_rw_session(slot)?;
2486+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
2487+
2488+
let key: [u8; 16] = [
2489+
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f,
2490+
0x3c,
2491+
];
2492+
2493+
// Can not create object with Unique Id
2494+
let key_template = vec![
2495+
Attribute::Class(ObjectClass::SECRET_KEY),
2496+
Attribute::KeyType(KeyType::AES),
2497+
Attribute::Token(true),
2498+
Attribute::Sensitive(true),
2499+
Attribute::Private(true),
2500+
Attribute::Value(key.into()),
2501+
Attribute::UniqueId(vec![0x00, 0x00, 0x00, 0x01]),
2502+
];
2503+
let res = session.create_object(&key_template);
2504+
assert!(res.is_err());
2505+
assert!(matches!(
2506+
res,
2507+
Err(Error::Pkcs11(
2508+
RvError::AttributeTypeInvalid,
2509+
Function::CreateObject
2510+
))
2511+
));
2512+
2513+
let generate_template = vec![
2514+
Attribute::Token(true),
2515+
Attribute::ValueLen(32.into()),
2516+
Attribute::Encrypt(true),
2517+
];
2518+
2519+
// generate a secret key
2520+
let key = session.generate_key(&Mechanism::AesKeyGen, &generate_template)?;
2521+
2522+
// we can get the UniqueId attribute
2523+
let attrs = session.get_attributes(key, &[AttributeType::UniqueId])?;
2524+
if is_softhsm() {
2525+
// SoftHSM does not support this attribute at all
2526+
assert_eq!(attrs.len(), 0);
2527+
} else {
2528+
assert!(matches!(attrs.first(), Some(Attribute::UniqueId(_))));
2529+
}
2530+
2531+
// we can not set the UniqueId attribute
2532+
let update_template = vec![Attribute::UniqueId(vec![0x01, 0x02, 0x03])];
2533+
let res = session.update_attributes(key, &update_template);
2534+
assert!(res.is_err());
2535+
if is_softhsm() {
2536+
// SoftHSM does not support this attribute at all
2537+
assert!(matches!(
2538+
res,
2539+
Err(Error::Pkcs11(
2540+
RvError::AttributeTypeInvalid,
2541+
Function::SetAttributeValue
2542+
))
2543+
));
2544+
} else {
2545+
assert!(matches!(
2546+
res,
2547+
Err(Error::Pkcs11(
2548+
RvError::AttributeReadOnly,
2549+
Function::SetAttributeValue
2550+
))
2551+
));
2552+
}
2553+
2554+
session.destroy_object(key)?;
2555+
2556+
Ok(())
2557+
}

0 commit comments

Comments
 (0)