Skip to content

Commit a66ed53

Browse files
simo5Gemini
andcommitted
Refactor object module into submodules
Split the `src/object` implementation into distinct submodules for better organization: - `factory`: Core object factory logic and registry. - `key`: Key-specific traits and factories. - `certs`: Certificate and trust object factories. Rename `CommonKeyFactory` to `KeyFactory` and move key-related methods into it. Update `ObjectFactory` to allow casting to specific factory traits via methods like `as_key_factory` and update call sites accordingly. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
1 parent 503104b commit a66ed53

File tree

17 files changed

+2497
-2442
lines changed

17 files changed

+2497
-2442
lines changed

src/aes.rs

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub(crate) fn check_key_len(len: usize) -> Result<()> {
8686
/// [AES secret key objects](https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203476)
8787
/// (Version 3.1)
8888
///
89-
/// Derives from the generic ObjectFactory, CommonKeyFactory and
89+
/// Derives from the generic ObjectFactory, KeyFactory and
9090
/// SecretKeyFactory
9191
///
9292
/// This is used to store the list of attributes allowed for an AES Key object, as well as provide
@@ -135,8 +135,44 @@ impl ObjectFactory for AesKeyFactory {
135135
Ok(obj)
136136
}
137137

138+
fn get_data(&self) -> &ObjectFactoryData {
139+
&self.data
140+
}
141+
fn get_data_mut(&mut self) -> &mut ObjectFactoryData {
142+
&mut self.data
143+
}
144+
145+
fn as_key_factory(&self) -> Result<&dyn KeyFactory> {
146+
Ok(self)
147+
}
148+
149+
fn as_secret_key_factory(&self) -> Result<&dyn SecretKeyFactory> {
150+
Ok(self)
151+
}
152+
}
153+
154+
impl KeyFactory for AesKeyFactory {
155+
/// The AES derive adds key length checks on top of the generic secret
156+
/// derive helper
157+
fn key_derive(
158+
&self,
159+
template: &[CK_ATTRIBUTE],
160+
161+
origin: &Object,
162+
) -> Result<Object> {
163+
let obj = self.internal_key_derive(template, origin)?;
164+
165+
let key_len = self.get_key_len(&obj);
166+
if key_len != 0 {
167+
if check_key_len(key_len).is_err() {
168+
return Err(CKR_TEMPLATE_INCONSISTENT)?;
169+
}
170+
}
171+
Ok(obj)
172+
}
173+
138174
fn export_for_wrapping(&self, key: &Object) -> Result<Vec<u8>> {
139-
SecretKeyFactory::export_for_wrapping(self, key)
175+
SecretKeyFactory::default_export_for_wrapping(self, key)
140176
}
141177

142178
fn import_from_wrapped(
@@ -166,42 +202,10 @@ impl ObjectFactory for AesKeyFactory {
166202
return Err(e);
167203
}
168204
}
169-
SecretKeyFactory::import_from_wrapped(self, data, template)
170-
}
171-
172-
/// The AES derive adds key length checks on top of the generic secret
173-
/// derive helper
174-
fn default_key_derive(
175-
&self,
176-
template: &[CK_ATTRIBUTE],
177-
origin: &Object,
178-
) -> Result<Object> {
179-
let obj = self.internal_key_derive(template, origin)?;
180-
181-
let key_len = self.get_key_len(&obj);
182-
if key_len != 0 {
183-
if check_key_len(key_len).is_err() {
184-
return Err(CKR_TEMPLATE_INCONSISTENT)?;
185-
}
186-
}
187-
Ok(obj)
188-
}
189-
190-
fn as_secret_key_factory(&self) -> Result<&dyn SecretKeyFactory> {
191-
Ok(self)
192-
}
193-
194-
fn get_data(&self) -> &ObjectFactoryData {
195-
&self.data
196-
}
197-
198-
fn get_data_mut(&mut self) -> &mut ObjectFactoryData {
199-
&mut self.data
205+
SecretKeyFactory::default_import_from_wrapped(self, data, template)
200206
}
201207
}
202208

203-
impl CommonKeyFactory for AesKeyFactory {}
204-
205209
impl SecretKeyFactory for AesKeyFactory {
206210
/// Helper to set key that check the key is correctly formed for an
207211
/// AES key object
@@ -303,7 +307,8 @@ impl Mechanism for AesMechanism {
303307
if mech.mechanism != CKM_AES_KEY_GEN {
304308
return Err(CKR_MECHANISM_INVALID)?;
305309
}
306-
let mut key = AES_KEY_FACTORY.default_key_generate(template)?;
310+
let mut key =
311+
AES_KEY_FACTORY.as_key_factory()?.key_generate(template)?;
307312
key.ensure_ulong(CKA_CLASS, CKO_SECRET_KEY)
308313
.map_err(|_| CKR_TEMPLATE_INCONSISTENT)?;
309314
key.ensure_ulong(CKA_KEY_TYPE, CKK_AES)
@@ -330,11 +335,10 @@ impl Mechanism for AesMechanism {
330335
if self.info.flags & CKF_WRAP != CKF_WRAP {
331336
return Err(CKR_MECHANISM_INVALID)?;
332337
}
333-
334338
AesOperation::wrap(
335339
mech,
336340
wrapping_key,
337-
key_template.export_for_wrapping(key)?,
341+
key_template.as_key_factory()?.export_for_wrapping(key)?,
338342
data,
339343
)
340344
}
@@ -356,7 +360,9 @@ impl Mechanism for AesMechanism {
356360
return Err(CKR_MECHANISM_INVALID)?;
357361
}
358362
let keydata = AesOperation::unwrap(mech, wrapping_key, data)?;
359-
key_template.import_from_wrapped(keydata, template)
363+
key_template
364+
.as_key_factory()?
365+
.import_from_wrapped(keydata, template)
360366
}
361367

362368
/// Implements the AES derivation operation
@@ -622,10 +628,9 @@ impl Derive for AesKDFOperation<'_> {
622628
}
623629
self.finalized = true;
624630
key.check_key_ops(CKO_SECRET_KEY, CKK_AES, CKA_DERIVE)?;
625-
626631
let factory =
627632
objfactories.get_obj_factory_from_key_template(template)?;
628-
let mut obj = factory.default_key_derive(template, key)?;
633+
let mut obj = factory.as_key_factory()?.key_derive(template, key)?;
629634

630635
let mechanism = CK_MECHANISM {
631636
mechanism: self.mech,

src/ec/ecdsa.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,20 @@ impl ObjectFactory for ECDSAPubFactory {
186186
fn get_data(&self) -> &ObjectFactoryData {
187187
&self.data
188188
}
189-
190189
fn get_data_mut(&mut self) -> &mut ObjectFactoryData {
191190
&mut self.data
192191
}
193192

193+
fn as_key_factory(&self) -> Result<&dyn KeyFactory> {
194+
Ok(self)
195+
}
196+
194197
fn as_public_key_factory(&self) -> Result<&dyn PubKeyFactory> {
195198
Ok(self)
196199
}
197200
}
198201

199-
impl CommonKeyFactory for ECDSAPubFactory {}
202+
impl KeyFactory for ECDSAPubFactory {}
200203

201204
impl PubKeyFactory for ECDSAPubFactory {
202205
fn pub_from_private<'a>(
@@ -251,6 +254,7 @@ impl ECDSAPrivFactory {
251254
factory
252255
}
253256
}
257+
254258
impl ObjectFactory for ECDSAPrivFactory {
255259
/// Creates an ECDSA Private-Key Object from a template
256260
///
@@ -315,30 +319,19 @@ impl ObjectFactory for ECDSAPrivFactory {
315319
Ok(obj)
316320
}
317321

318-
fn export_for_wrapping(&self, key: &Object) -> Result<Vec<u8>> {
319-
PrivKeyFactory::export_for_wrapping(self, key)
320-
}
321-
322-
fn import_from_wrapped(
323-
&self,
324-
data: Vec<u8>,
325-
template: &[CK_ATTRIBUTE],
326-
) -> Result<Object> {
327-
PrivKeyFactory::import_from_wrapped(self, data, template)
328-
}
329-
330322
fn get_data(&self) -> &ObjectFactoryData {
331323
&self.data
332324
}
333-
334325
fn get_data_mut(&mut self) -> &mut ObjectFactoryData {
335326
&mut self.data
336327
}
337-
}
338328

339-
impl CommonKeyFactory for ECDSAPrivFactory {}
329+
fn as_key_factory(&self) -> Result<&dyn KeyFactory> {
330+
Ok(self)
331+
}
332+
}
340333

341-
impl PrivKeyFactory for ECDSAPrivFactory {
334+
impl KeyFactory for ECDSAPrivFactory {
342335
fn export_for_wrapping(&self, key: &Object) -> Result<Vec<u8>> {
343336
export_for_wrapping(key)
344337
}
@@ -348,10 +341,12 @@ impl PrivKeyFactory for ECDSAPrivFactory {
348341
data: Vec<u8>,
349342
template: &[CK_ATTRIBUTE],
350343
) -> Result<Object> {
351-
import_from_wrapped(CKK_EC, data, self.default_key_unwrap(template)?)
344+
import_from_wrapped(CKK_EC, data, self.key_unwrap(template)?)
352345
}
353346
}
354347

348+
impl PrivKeyFactory for ECDSAPrivFactory {}
349+
355350
/// Object that represents CKK_EC related mechanisms
356351
#[derive(Debug)]
357352
pub struct EcdsaMechanism {
@@ -422,17 +417,19 @@ impl Mechanism for EcdsaMechanism {
422417
pubkey_template: &[CK_ATTRIBUTE],
423418
prikey_template: &[CK_ATTRIBUTE],
424419
) -> Result<(Object, Object)> {
425-
let mut pubkey =
426-
PUBLIC_KEY_FACTORY.default_key_generate(pubkey_template)?;
420+
let mut pubkey = PUBLIC_KEY_FACTORY
421+
.as_key_factory()?
422+
.key_generate(pubkey_template)?;
427423
pubkey
428424
.ensure_ulong(CKA_CLASS, CKO_PUBLIC_KEY)
429425
.map_err(|_| CKR_TEMPLATE_INCONSISTENT)?;
430426
pubkey
431427
.ensure_ulong(CKA_KEY_TYPE, CKK_EC)
432428
.map_err(|_| CKR_TEMPLATE_INCONSISTENT)?;
433429

434-
let mut privkey =
435-
PRIVATE_KEY_FACTORY.default_key_generate(prikey_template)?;
430+
let mut privkey = PRIVATE_KEY_FACTORY
431+
.as_key_factory()?
432+
.key_generate(prikey_template)?;
436433
privkey
437434
.ensure_ulong(CKA_CLASS, CKO_PRIVATE_KEY)
438435
.map_err(|_| CKR_TEMPLATE_INCONSISTENT)?;

src/ec/eddsa.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,20 @@ impl ObjectFactory for EDDSAPubFactory {
167167
fn get_data(&self) -> &ObjectFactoryData {
168168
&self.data
169169
}
170-
171170
fn get_data_mut(&mut self) -> &mut ObjectFactoryData {
172171
&mut self.data
173172
}
174173

174+
fn as_key_factory(&self) -> Result<&dyn KeyFactory> {
175+
Ok(self)
176+
}
177+
175178
fn as_public_key_factory(&self) -> Result<&dyn PubKeyFactory> {
176179
Ok(self)
177180
}
178181
}
179182

180-
impl CommonKeyFactory for EDDSAPubFactory {}
183+
impl KeyFactory for EDDSAPubFactory {}
181184

182185
impl PubKeyFactory for EDDSAPubFactory {
183186
fn pub_from_private<'a>(
@@ -289,30 +292,19 @@ impl ObjectFactory for EDDSAPrivFactory {
289292
Ok(obj)
290293
}
291294

292-
fn export_for_wrapping(&self, key: &Object) -> Result<Vec<u8>> {
293-
PrivKeyFactory::export_for_wrapping(self, key)
294-
}
295-
296-
fn import_from_wrapped(
297-
&self,
298-
data: Vec<u8>,
299-
template: &[CK_ATTRIBUTE],
300-
) -> Result<Object> {
301-
PrivKeyFactory::import_from_wrapped(self, data, template)
302-
}
303-
304295
fn get_data(&self) -> &ObjectFactoryData {
305296
&self.data
306297
}
307-
308298
fn get_data_mut(&mut self) -> &mut ObjectFactoryData {
309299
&mut self.data
310300
}
311-
}
312301

313-
impl CommonKeyFactory for EDDSAPrivFactory {}
302+
fn as_key_factory(&self) -> Result<&dyn KeyFactory> {
303+
Ok(self)
304+
}
305+
}
314306

315-
impl PrivKeyFactory for EDDSAPrivFactory {
307+
impl KeyFactory for EDDSAPrivFactory {
316308
fn export_for_wrapping(&self, key: &Object) -> Result<Vec<u8>> {
317309
export_for_wrapping(key)
318310
}
@@ -322,14 +314,12 @@ impl PrivKeyFactory for EDDSAPrivFactory {
322314
data: Vec<u8>,
323315
template: &[CK_ATTRIBUTE],
324316
) -> Result<Object> {
325-
import_from_wrapped(
326-
CKK_EC_EDWARDS,
327-
data,
328-
self.default_key_unwrap(template)?,
329-
)
317+
import_from_wrapped(CKK_EC_EDWARDS, data, self.key_unwrap(template)?)
330318
}
331319
}
332320

321+
impl PrivKeyFactory for EDDSAPrivFactory {}
322+
333323
/// Object that represents EdDSA related mechanisms
334324
335325
#[derive(Debug)]
@@ -396,17 +386,19 @@ impl Mechanism for EddsaMechanism {
396386
pubkey_template: &[CK_ATTRIBUTE],
397387
prikey_template: &[CK_ATTRIBUTE],
398388
) -> Result<(Object, Object)> {
399-
let mut pubkey =
400-
PUBLIC_KEY_FACTORY.default_key_generate(pubkey_template)?;
389+
let mut pubkey = PUBLIC_KEY_FACTORY
390+
.as_key_factory()?
391+
.key_generate(pubkey_template)?;
401392
pubkey
402393
.ensure_ulong(CKA_CLASS, CKO_PUBLIC_KEY)
403394
.map_err(|_| CKR_TEMPLATE_INCONSISTENT)?;
404395
pubkey
405396
.ensure_ulong(CKA_KEY_TYPE, CKK_EC_EDWARDS)
406397
.map_err(|_| CKR_TEMPLATE_INCONSISTENT)?;
407398

408-
let mut privkey =
409-
PRIVATE_KEY_FACTORY.default_key_generate(prikey_template)?;
399+
let mut privkey = PRIVATE_KEY_FACTORY
400+
.as_key_factory()?
401+
.key_generate(prikey_template)?;
410402
privkey
411403
.ensure_ulong(CKA_CLASS, CKO_PRIVATE_KEY)
412404
.map_err(|_| CKR_TEMPLATE_INCONSISTENT)?;

0 commit comments

Comments
 (0)