Skip to content

Commit c2e82bd

Browse files
simo5Gemini
andcommitted
Refactor builtin object creation logic
Introduce a `builtin_create` method to the `ObjectFactory` trait to standardize the creation of static objects like FIPS indicators, Profiles, and Mechanisms. Move the construction logic for these objects from ad-hoc helper functions (e.g., `insert_fips_validation`) into their respective factories. This simplifies the token initialization code by using a generic `insert_builtin_object` helper. Also rename `internal_object_derive` to `internal_key_derive` and adjust related key creation helpers to clearer names. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
1 parent f94eb1b commit c2e82bd

File tree

5 files changed

+176
-136
lines changed

5 files changed

+176
-136
lines changed

src/aes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl ObjectFactory for AesKeyFactory {
176176
template: &[CK_ATTRIBUTE],
177177
origin: &Object,
178178
) -> Result<Object> {
179-
let obj = self.internal_object_derive(template, origin)?;
179+
let obj = self.internal_key_derive(template, origin)?;
180180

181181
let key_len = self.get_key_len(&obj);
182182
if key_len != 0 {

src/fips/indicators.rs

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
use std::sync::LazyLock;
55

6-
use crate::attribute::Attribute;
6+
use crate::attribute::{Attribute, CkAttrs};
77
use crate::ec::{get_oid_from_obj, oid_to_bits};
88
use crate::error::Result;
99
use crate::object::*;
1010
use crate::pkcs11::vendor::KRY_UNSPEC;
1111
use crate::pkcs11::*;
12-
use crate::Token;
1312

1413
/// The flag returned in the CKA_VALIDATION_FLAG attribute
1514
///
@@ -21,8 +20,19 @@ use crate::Token;
2120
/// to mark operations. Applications need to get the flag value after
2221
/// token initialization and use that value thereafter to check against
2322
/// objects and session CKA_VALIDATION_FLAGS attributes.
23+
2424
pub const KRF_FIPS: CK_ULONG = 1;
2525

26+
/* TODO: These should be generated at build time */
27+
const VALIDATION_VERSION: [u8; 2] = [3u8, 0u8];
28+
const VALIDATION_LEVEL: CK_ULONG = 1;
29+
const MODULE_ID: &str = "Kryoptic FIPS Module - v1";
30+
const COUNTRY: &str = "US";
31+
const CERTIFICATE: &str = "Pending";
32+
const CERTIFICATE_URI: &str = "";
33+
const VENDOR_URI: &str = "https://github.com/latchset/kryoptic";
34+
const PROFILE: &str = "";
35+
2636
/// The Validation Object factory
2737
#[derive(Debug)]
2838
pub struct ValidationFactory {
@@ -36,7 +46,7 @@ impl ValidationFactory {
3646
data: ObjectFactoryData::new(CKO_VALIDATION),
3747
};
3848

39-
factory.add_common_storage_attrs(false);
49+
factory.add_common_object_attrs();
4050

4151
let attributes = factory.data.get_attributes_mut();
4252

@@ -92,6 +102,57 @@ impl ValidationFactory {
92102
}
93103

94104
impl ObjectFactory for ValidationFactory {
105+
fn create(&self, _template: &[CK_ATTRIBUTE]) -> Result<Object> {
106+
Err(CKR_TEMPLATE_INCOMPLETE)?
107+
}
108+
109+
fn builtin_create(&self, stable_id: CK_ULONG) -> Result<Object> {
110+
let mut tmpl = CkAttrs::with_capacity(11);
111+
match stable_id {
112+
super::FIPS_VALIDATION_OBJ => {
113+
tmpl.add_ulong(CKA_VALIDATION_TYPE, &CKV_TYPE_SOFTWARE);
114+
tmpl.add_slice(CKA_VALIDATION_VERSION, &VALIDATION_VERSION)?;
115+
tmpl.add_ulong(CKA_VALIDATION_LEVEL, &VALIDATION_LEVEL);
116+
tmpl.add_slice(CKA_VALIDATION_MODULE_ID, MODULE_ID.as_bytes())?;
117+
tmpl.add_ulong(CKA_VALIDATION_FLAG, &KRF_FIPS);
118+
tmpl.add_ulong(
119+
CKA_VALIDATION_AUTHORITY_TYPE,
120+
&CKV_AUTHORITY_TYPE_NIST_CMVP,
121+
);
122+
tmpl.add_slice(CKA_VALIDATION_COUNTRY, COUNTRY.as_bytes())?;
123+
tmpl.add_slice(
124+
CKA_VALIDATION_CERTIFICATE_IDENTIFIER,
125+
CERTIFICATE.as_bytes(),
126+
)?;
127+
tmpl.add_slice(
128+
CKA_VALIDATION_CERTIFICATE_URI,
129+
CERTIFICATE_URI.as_bytes(),
130+
)?;
131+
tmpl.add_slice(
132+
CKA_VALIDATION_VENDOR_URI,
133+
VENDOR_URI.as_bytes(),
134+
)?;
135+
tmpl.add_slice(CKA_VALIDATION_PROFILE, PROFILE.as_bytes())?;
136+
}
137+
_ => return Err(CKR_GENERAL_ERROR)?,
138+
}
139+
let mut obj = self.internal_object_create(
140+
tmpl.as_slice(),
141+
OAFlags::empty(),
142+
OAFlags::RequiredOnCreate,
143+
)?;
144+
obj.generate_stable_unique(stable_id);
145+
Ok(obj)
146+
}
147+
148+
fn copy(
149+
&self,
150+
_origin: &Object,
151+
_template: &[CK_ATTRIBUTE],
152+
) -> Result<Object> {
153+
Err(CKR_TEMPLATE_INCOMPLETE)?
154+
}
155+
95156
/// Helper method to get a reference to the ObjectFactoryData
96157
fn get_data(&self) -> &ObjectFactoryData {
97158
&self.data
@@ -110,67 +171,6 @@ impl ObjectFactory for ValidationFactory {
110171
pub(crate) static VALIDATION_FACTORY: LazyLock<Box<dyn ObjectFactory>> =
111172
LazyLock::new(|| Box::new(ValidationFactory::new()));
112173

113-
/// Synthesize a FIPS CKO_VALIDATION object
114-
///
115-
/// This is generally done only once at token initialization
116-
pub fn insert_fips_validation(token: &mut Token) -> Result<()> {
117-
let mut obj = Object::new(CKO_VALIDATION);
118-
obj.set_attr(Attribute::from_bool(CKA_TOKEN, false))?;
119-
obj.set_attr(Attribute::from_bool(CKA_DESTROYABLE, false))?;
120-
obj.set_attr(Attribute::from_bool(CKA_MODIFIABLE, false))?;
121-
obj.set_attr(Attribute::from_bool(CKA_PRIVATE, false))?;
122-
obj.set_attr(Attribute::from_bool(CKA_SENSITIVE, false))?;
123-
obj.set_attr(Attribute::from_ulong(
124-
CKA_VALIDATION_TYPE,
125-
CKV_TYPE_SOFTWARE,
126-
))?;
127-
obj.set_attr(Attribute::from_bytes(
128-
CKA_VALIDATION_VERSION,
129-
vec![3u8, 0u8],
130-
))?;
131-
obj.set_attr(Attribute::from_ulong(CKA_VALIDATION_LEVEL, 1))?;
132-
/* TODO: This should be generated at build time */
133-
obj.set_attr(Attribute::from_string(
134-
CKA_VALIDATION_MODULE_ID,
135-
String::from("Kryoptic FIPS Module - v1"),
136-
))?;
137-
obj.set_attr(Attribute::from_ulong(CKA_VALIDATION_FLAG, KRF_FIPS))?;
138-
obj.set_attr(Attribute::from_ulong(
139-
CKA_VALIDATION_AUTHORITY_TYPE,
140-
CKV_AUTHORITY_TYPE_NIST_CMVP,
141-
))?;
142-
143-
/* TODO: The following attributes should all be determined at build time */
144-
obj.set_attr(Attribute::from_string(
145-
CKA_VALIDATION_COUNTRY,
146-
String::from("US"),
147-
))?;
148-
obj.set_attr(Attribute::from_string(
149-
CKA_VALIDATION_CERTIFICATE_IDENTIFIER,
150-
String::from("Pending"),
151-
))?;
152-
obj.set_attr(Attribute::from_string(
153-
CKA_VALIDATION_CERTIFICATE_URI,
154-
String::from(""),
155-
))?;
156-
obj.set_attr(Attribute::from_string(
157-
CKA_VALIDATION_VENDOR_URI,
158-
String::from("https://github.com/latchset/kryoptic"),
159-
))?;
160-
obj.set_attr(Attribute::from_string(
161-
CKA_VALIDATION_PROFILE,
162-
String::from(""),
163-
))?;
164-
165-
/* generate a unique but stable id */
166-
obj.generate_stable_unique(1);
167-
168-
/* invalid session handle will prevent it from being removed when
169-
* session objects are cleared on session closings */
170-
let _ = token.insert_object(CK_INVALID_HANDLE, obj)?;
171-
Ok(())
172-
}
173-
174174
/// Helper to convert bits to bytes
175175
macro_rules! btb {
176176
($val:expr) => {

src/fips/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ use crate::error::Result;
66
use crate::mechanism::Mechanisms;
77
use crate::object::{ObjectFactories, ObjectType};
88
use crate::pkcs11::*;
9-
use crate::token::Token;
109

1110
use ossl::fips;
1211

1312
pub(crate) mod indicators;
1413
pub(crate) mod kats;
1514

15+
pub const FIPS_VALIDATION_OBJ: CK_ULONG = 1;
16+
1617
/// Sets the FIPS module into the error state
1718
pub fn set_fips_error_state() {
1819
fips::set_error_state();
@@ -23,11 +24,6 @@ pub fn check_fips_state_ok() -> bool {
2324
return fips::check_state_ok();
2425
}
2526

26-
/// Helper function to set up validation objects at token initialization
27-
pub fn token_init(token: &mut Token) -> Result<()> {
28-
indicators::insert_fips_validation(token)
29-
}
30-
3127
/// Helper function to register the validation object factory
3228
pub fn register(_: &mut Mechanisms, ot: &mut ObjectFactories) {
3329
ot.add_factory(

0 commit comments

Comments
 (0)