Skip to content

Commit 71b3711

Browse files
author
İlker BALTACI
committed
Unhandled attribute defined for undefined attributes. ECKdf struct changed for KDF type and shared value definition.
Signed-off-by: İlker BALTACI <[email protected]>
1 parent c45a81b commit 71b3711

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

cryptoki/src/mechanism/elliptic_curve.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ use std::ptr;
1919
#[repr(C)]
2020
pub struct Ecdh1DeriveParams<'a> {
2121
/// Key derivation function
22-
kdf: CK_EC_KDF_TYPE,
22+
pub kdf: CK_EC_KDF_TYPE,
2323
/// Length of the optional shared data used by some of the key
2424
/// derivation functions
25-
shared_data_len: Ulong,
25+
pub shared_data_len: Ulong,
2626
/// Address of the optional data or `std::ptr::null()` of there is
2727
/// no shared data
28-
shared_data: *const u8,
28+
pub shared_data: *const u8,
2929
/// Length of the other party's public key
30-
public_data_len: Ulong,
30+
pub public_data_len: Ulong,
3131
/// Pointer to the other party public key
32-
public_data: *const u8,
32+
pub public_data: *const u8,
3333
/// Marker type to ensure we don't outlive shared and public data
3434
_marker: PhantomData<&'a [u8]>,
3535
}
@@ -82,7 +82,15 @@ pub struct EcKdf<'a> {
8282
shared_data: Option<&'a [u8]>,
8383
}
8484

85-
impl EcKdf<'_> {
85+
impl<'a> EcKdf<'a> {
86+
/// Define KDF_TYPE and shared_data
87+
pub fn new(kdf_type: CK_EC_KDF_TYPE, shared_data: Option<&'a [u8]>) -> Self {
88+
Self {
89+
kdf_type,
90+
shared_data,
91+
}
92+
}
93+
8694
/// The null transformation. The derived key value is produced by
8795
/// taking bytes from the left of the agreed value. The new key
8896
/// size is limited to the size of the agreed value.

cryptoki/src/mechanism/rsa.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ pub struct PkcsPssParams {
137137
pub struct PkcsOaepParams<'a> {
138138
/// mechanism ID of the message digest algorithm used to calculate the digest of the encoding
139139
/// parameter
140-
hash_alg: MechanismType,
140+
pub hash_alg: MechanismType,
141141
/// mask generation function to use on the encoded block
142-
mgf: PkcsMgfType,
142+
pub mgf: PkcsMgfType,
143143
/// source of the encoding parameter
144-
source: CK_RSA_PKCS_OAEP_SOURCE_TYPE,
144+
pub source: CK_RSA_PKCS_OAEP_SOURCE_TYPE,
145145
/// data used as the input for the encoding parameter source
146-
source_data: *const c_void,
146+
pub source_data: *const c_void,
147147
/// length of the encoding parameter source input
148-
source_data_len: Ulong,
148+
pub source_data_len: Ulong,
149149
/// marker type to ensure we don't outlive the source_data
150150
_marker: PhantomData<&'a [u8]>,
151151
}

cryptoki/src/object.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::error::{Error, Result};
66
use crate::mechanism::MechanismType;
77
use crate::types::{Date, Ulong};
88
use cryptoki_sys::*;
9-
use log::error;
9+
use log::{error, warn};
1010
use std::convert::TryFrom;
1111
use std::convert::TryInto;
1212
use std::ffi::c_void;
@@ -136,6 +136,8 @@ pub enum AttributeType {
136136
Wrap,
137137
/// Indicates that the key can only be wrapped with a wrapping key that has the Trusted attribute
138138
WrapWithTrusted,
139+
/// Wraps undefined and custom attribute types.
140+
Unhandled(CK_ATTRIBUTE_TYPE),
139141
}
140142

141143
impl AttributeType {
@@ -328,6 +330,7 @@ impl From<AttributeType> for CK_ATTRIBUTE_TYPE {
328330
AttributeType::VerifyRecover => CKA_VERIFY_RECOVER,
329331
AttributeType::Wrap => CKA_WRAP,
330332
AttributeType::WrapWithTrusted => CKA_WRAP_WITH_TRUSTED,
333+
AttributeType::Unhandled(attr_type) => attr_type,
331334
}
332335
}
333336
}
@@ -397,8 +400,10 @@ impl TryFrom<CK_ATTRIBUTE_TYPE> for AttributeType {
397400
CKA_WRAP => Ok(AttributeType::Wrap),
398401
CKA_WRAP_WITH_TRUSTED => Ok(AttributeType::WrapWithTrusted),
399402
attr_type => {
400-
error!("Attribute type {} not supported.", attr_type);
401-
Err(Error::NotSupported)
403+
// error!("Attribute type {} not supported.", attr_type);
404+
// Err(Error::NotSupported)
405+
warn!("Unhandled attribute type {} detected.", attr_type);
406+
Ok(AttributeType::Unhandled(attr_type))
402407
}
403408
}
404409
}
@@ -526,6 +531,8 @@ pub enum Attribute {
526531
Wrap(bool),
527532
/// Indicates that the key can only be wrapped with a wrapping key that has the Trusted attribute
528533
WrapWithTrusted(bool),
534+
/// Not defined attributes enter this option.
535+
Unhandled(CK_ATTRIBUTE_TYPE, Vec<u8>),
529536
}
530537

531538
impl Attribute {
@@ -591,6 +598,9 @@ impl Attribute {
591598
Attribute::VerifyRecover(_) => AttributeType::VerifyRecover,
592599
Attribute::Wrap(_) => AttributeType::Wrap,
593600
Attribute::WrapWithTrusted(_) => AttributeType::WrapWithTrusted,
601+
Attribute::Unhandled(attribute_type, _) => {
602+
AttributeType::Unhandled(attribute_type.clone())
603+
}
594604
}
595605
}
596606

@@ -658,6 +668,7 @@ impl Attribute {
658668
Attribute::AllowedMechanisms(mechanisms) => {
659669
size_of::<CK_MECHANISM_TYPE>() * mechanisms.len()
660670
}
671+
Attribute::Unhandled(_, bytes) => bytes.len(),
661672
}
662673
}
663674

@@ -730,7 +741,8 @@ impl Attribute {
730741
| Attribute::Subject(bytes)
731742
| Attribute::Url(bytes)
732743
| Attribute::Value(bytes)
733-
| Attribute::Id(bytes) => bytes.as_ptr() as *mut c_void,
744+
| Attribute::Id(bytes)
745+
| Attribute::Unhandled(_, bytes) => bytes.as_ptr() as *mut c_void,
734746
// Unique types
735747
Attribute::CertificateType(certificate_type) => {
736748
certificate_type as *const _ as *mut c_void
@@ -930,6 +942,7 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
930942
}
931943
}
932944
}
945+
AttributeType::Unhandled(_) => todo!(),
933946
}
934947
}
935948
}
@@ -1006,6 +1019,8 @@ impl ObjectClass {
10061019
pub const MECHANISM: ObjectClass = ObjectClass { val: CKO_MECHANISM };
10071020
/// An OTP key object
10081021
pub const OTP_KEY: ObjectClass = ObjectClass { val: CKO_OTP_KEY };
1022+
/// A profile object
1023+
pub const PROFILE: ObjectClass = ObjectClass { val: CKO_PROFILE };
10091024

10101025
pub(crate) fn stringify(class: CK_OBJECT_CLASS) -> String {
10111026
match class {
@@ -1018,6 +1033,7 @@ impl ObjectClass {
10181033
CKO_DOMAIN_PARAMETERS => String::from(stringify!(CKO_DOMAIN_PARAMETERS)),
10191034
CKO_MECHANISM => String::from(stringify!(CKO_MECHANISM)),
10201035
CKO_OTP_KEY => String::from(stringify!(CKO_OTP_KEY)),
1036+
CKO_PROFILE => String::from(stringify!(CKO_PROFILE)),
10211037
_ => format!("unknown ({class:08x})"),
10221038
}
10231039
}

0 commit comments

Comments
 (0)