Skip to content

Commit 862b392

Browse files
committed
Adding helper methods for CoMID.
Signed-off-by: Larry Dewey <[email protected]>
1 parent fdc0e8b commit 862b392

File tree

7 files changed

+278
-121
lines changed

7 files changed

+278
-121
lines changed

src/comid.rs

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ generate_tagged!((
107107
"A Concise Module Identifier (CoMID) structured tag"
108108
),);
109109
/// A Concise Module Identifier (CoMID) tag structure tagged with CBOR tag 506
110-
#[derive(Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord)]
110+
#[derive(
111+
Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
112+
)]
111113
#[repr(C)]
112114
pub struct ConciseMidTag<'a> {
113115
/// Optional language identifier for the tag content
@@ -189,7 +191,7 @@ impl<'a> ConciseMidTag<'a> {
189191
/// - If reference triples exist but none match the environment, a new triple is added
190192
pub fn add_reference_value<T>(
191193
&mut self,
192-
environment: EnvironmentMap<'a>,
194+
environment: &EnvironmentMap<'a>,
193195
mkey: Option<MeasuredElementTypeChoice<'a>>,
194196
value: &T,
195197
) -> Result<(), std::io::Error>
@@ -216,13 +218,13 @@ impl<'a> ConciseMidTag<'a> {
216218
match &mut self.triples.reference_triples {
217219
None => {
218220
let new_record = ReferenceTripleRecord {
219-
ref_env: environment,
221+
ref_env: environment.clone(),
220222
ref_claims: measurement.into(),
221223
};
222224
self.triples.reference_triples = Some(OneOrMany::One(new_record));
223225
}
224226
Some(OneOrMany::One(record)) => {
225-
if record.ref_env == environment {
227+
if record.ref_env == *environment {
226228
match &mut record.ref_claims {
227229
OneOrMany::One(original_claim) => {
228230
record.ref_claims =
@@ -232,7 +234,7 @@ impl<'a> ConciseMidTag<'a> {
232234
}
233235
} else {
234236
let new_record: ReferenceTripleRecord<'a> = ReferenceTripleRecord {
235-
ref_env: environment,
237+
ref_env: environment.clone(),
236238
ref_claims: measurement.into(),
237239
};
238240

@@ -241,7 +243,7 @@ impl<'a> ConciseMidTag<'a> {
241243
}
242244
}
243245
Some(OneOrMany::Many(vec)) => {
244-
if let Some(record) = vec.iter_mut().find(|r| r.ref_env == environment) {
246+
if let Some(record) = vec.iter_mut().find(|r| r.ref_env == *environment) {
245247
match &mut record.ref_claims {
246248
OneOrMany::One(claim) => {
247249
record.ref_claims =
@@ -251,7 +253,7 @@ impl<'a> ConciseMidTag<'a> {
251253
}
252254
} else {
253255
let new_record = ReferenceTripleRecord {
254-
ref_env: environment,
256+
ref_env: environment.clone(),
255257
ref_claims: measurement.into(),
256258
};
257259
vec.push(new_record);
@@ -260,10 +262,85 @@ impl<'a> ConciseMidTag<'a> {
260262
}
261263
Ok(())
262264
}
265+
pub fn add_endorsement_value<T>(
266+
&mut self,
267+
environment: &EnvironmentMap<'a>,
268+
mkey: Option<MeasuredElementTypeChoice<'a>>,
269+
value: &T,
270+
) -> Result<(), std::io::Error>
271+
where
272+
T: ?Sized + Serialize,
273+
{
274+
let mut raw_bytes = vec![];
275+
ciborium::into_writer(value, &mut raw_bytes)
276+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
277+
let raw_value = TaggedBytes::new(raw_bytes);
278+
279+
let measurement = MeasurementMap {
280+
mkey,
281+
mval: MeasurementValuesMap {
282+
raw: Some(RawValueType {
283+
raw_value: raw_value.into(),
284+
raw_value_mask: None,
285+
}),
286+
..Default::default()
287+
},
288+
authorized_by: None,
289+
};
290+
291+
match &mut self.triples.endorse_triples {
292+
None => {
293+
let new_record = EndorsedTripleRecord {
294+
condition: environment.clone(),
295+
endorsement: measurement.into(),
296+
};
297+
self.triples.endorse_triples = Some(OneOrMany::One(new_record));
298+
}
299+
Some(OneOrMany::One(record)) => {
300+
if record.condition == *environment {
301+
match &mut record.endorsement {
302+
OneOrMany::One(original_claim) => {
303+
record.endorsement =
304+
OneOrMany::Many(vec![std::mem::take(original_claim), measurement])
305+
}
306+
OneOrMany::Many(claims) => claims.push(measurement),
307+
}
308+
} else {
309+
let new_record: EndorsedTripleRecord<'a> = EndorsedTripleRecord {
310+
condition: environment.clone(),
311+
endorsement: measurement.into(),
312+
};
313+
314+
let many = vec![std::mem::take(record), new_record];
315+
self.triples.endorse_triples = Some(OneOrMany::Many(many));
316+
}
317+
}
318+
Some(OneOrMany::Many(vec)) => {
319+
if let Some(record) = vec.iter_mut().find(|r| r.condition == *environment) {
320+
match &mut record.endorsement {
321+
OneOrMany::One(claim) => {
322+
record.endorsement =
323+
OneOrMany::Many(vec![std::mem::take(claim), measurement])
324+
}
325+
OneOrMany::Many(claims) => claims.push(measurement),
326+
}
327+
} else {
328+
let new_record = EndorsedTripleRecord {
329+
condition: environment.clone(),
330+
endorsement: measurement.into(),
331+
};
332+
vec.push(new_record);
333+
}
334+
}
335+
}
336+
Ok(())
337+
}
263338
}
264339

265340
/// Identification information for a tag
266-
#[derive(Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord)]
341+
#[derive(
342+
Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
343+
)]
267344
#[repr(C)]
268345
pub struct TagIdentityMap<'a> {
269346
/// Unique identifier for the tag
@@ -276,7 +353,7 @@ pub struct TagIdentityMap<'a> {
276353
}
277354

278355
/// Represents either a string or UUID tag identifier
279-
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord)]
356+
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord, Clone)]
280357
#[repr(C)]
281358
pub enum TagIdTypeChoice<'a> {
282359
/// Text string identifier
@@ -292,7 +369,9 @@ impl<'a> From<&'a str> for TagIdTypeChoice<'a> {
292369
}
293370

294371
/// Information about an entity associated with the tag
295-
#[derive(Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord)]
372+
#[derive(
373+
Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
374+
)]
296375
#[repr(C)]
297376
pub struct ComidEntityMap<'a> {
298377
/// Name of the entity
@@ -311,7 +390,7 @@ pub struct ComidEntityMap<'a> {
311390
}
312391

313392
/// Role types that can be assigned to entities
314-
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord)]
393+
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord, Clone)]
315394
#[repr(C)]
316395
pub enum ComidRoleTypeChoice {
317396
/// Entity that created the tag (value: 0)
@@ -323,7 +402,9 @@ pub enum ComidRoleTypeChoice {
323402
}
324403

325404
/// Reference to another tag and its relationship to this one
326-
#[derive(Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord)]
405+
#[derive(
406+
Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
407+
)]
327408
#[repr(C)]
328409
pub struct LinkedTagMap<'a> {
329410
/// Identifier of the linked tag
@@ -335,7 +416,7 @@ pub struct LinkedTagMap<'a> {
335416
}
336417

337418
/// Types of relationships between tags
338-
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord)]
419+
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord, Clone)]
339420
#[repr(C)]
340421
pub enum TagRelTypeChoice {
341422
/// This tag supplements the linked tag by providing additional information
@@ -347,7 +428,7 @@ pub enum TagRelTypeChoice {
347428
}
348429

349430
/// Collection of different types of triples describing the module characteristics
350-
#[derive(Default, Debug, Serialize, Deserialize, From, PartialEq, Eq, PartialOrd, Ord)]
431+
#[derive(Default, Debug, Serialize, Deserialize, From, PartialEq, Eq, PartialOrd, Ord, Clone)]
351432
#[repr(C)]
352433
pub struct TriplesMap<'a> {
353434
/// Optional reference triples that link to external references

src/core.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub type Int = i32;
6767
pub type Integer = Int;
6868

6969
/// ExtensionMap represents the possible types that can be used in extensions
70-
#[derive(Debug, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq, From, TryFrom)]
70+
#[derive(Debug, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq, From, TryFrom, Clone)]
7171
pub enum ExtensionMap<'a> {
7272
/// A UTF-8 string value
7373
Text(Text<'a>),
@@ -99,27 +99,23 @@ pub enum ExtensionMap<'a> {
9999
Eq,
100100
PartialOrd,
101101
Ord,
102+
Clone,
102103
)]
103-
pub struct UuidType {
104-
#[serde(flatten)]
105-
pub field: [u8; 16],
106-
}
104+
pub struct UuidType(pub FixedBytes<16>);
107105

108106
impl TryFrom<&[u8]> for UuidType {
109107
type Error = std::array::TryFromSliceError;
110108

111109
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
112-
let field = <[u8; 16]>::try_from(value)?;
113-
Ok(Self { field })
110+
Ok(Self(FixedBytes(value.try_into()?)))
114111
}
115112
}
116113

117114
/// UEID type representing a 33-byte Unique Entity Identifier
118-
#[derive(Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord)]
119-
pub struct UeidType {
120-
#[serde(flatten)]
121-
pub field: FixedBytes<33>,
122-
}
115+
#[derive(
116+
Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
117+
)]
118+
pub struct UeidType(pub FixedBytes<33>);
123119

124120
generate_tagged!(
125121
(1, IntegerTime, Int, "A representation of time in integer format using CBOR tag 1"),
@@ -142,7 +138,7 @@ generate_tagged!(
142138

143139
/// Represents a value that can be either text or bytes
144140
#[repr(C)]
145-
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord)]
141+
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord, Clone)]
146142
pub enum TextOrBytes<'a> {
147143
/// UTF-8 string value
148144
Text(Text<'a>),
@@ -152,7 +148,7 @@ pub enum TextOrBytes<'a> {
152148

153149
/// Represents a value that can be either text or fixed-size bytes
154150
#[repr(C)]
155-
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord)]
151+
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord, Clone)]
156152
pub enum TextOrBytesSized<'a, const N: usize> {
157153
/// UTF-8 string value
158154
Text(Text<'a>),
@@ -162,7 +158,9 @@ pub enum TextOrBytesSized<'a, const N: usize> {
162158

163159
/// Represents a hash entry with algorithm ID and hash value
164160
#[repr(C)]
165-
#[derive(Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord)]
161+
#[derive(
162+
Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
163+
)]
166164
pub struct HashEntry {
167165
/// Algorithm identifier for the hash
168166
#[serde(rename = "hash-alg-id")]
@@ -231,7 +229,6 @@ pub struct GlobalAttributes<'a> {
231229
#[serde(skip_serializing_if = "Option::is_none")]
232230
pub lang: Option<Text<'a>>,
233231
/// Arbitrary attributes
234-
#[serde(flatten)]
235232
pub attributes: BTreeMap<Label<'a>, AttributeValue<'a>>,
236233
}
237234

@@ -283,7 +280,9 @@ pub enum CotlMapRegistry {
283280

284281
/// Represents a digest value with its algorithm identifier
285282
#[repr(C)]
286-
#[derive(Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord)]
283+
#[derive(
284+
Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
285+
)]
287286
pub struct Digest<'a> {
288287
/// Algorithm identifier for the digest
289288
pub alg: AlgLabel<'a>,
@@ -293,7 +292,7 @@ pub struct Digest<'a> {
293292

294293
/// Represents either a COSE key set or a single COSE key
295294
#[repr(C)]
296-
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord)]
295+
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord, Clone)]
297296
pub enum CoseKeySetOrKey<'a> {
298297
/// A set of COSE keys
299298
KeySet(OneOrMany<CoseKey<'a>>),
@@ -302,7 +301,9 @@ pub enum CoseKeySetOrKey<'a> {
302301
}
303302

304303
/// Represents a COSE key structure as defined in RFC 8152
305-
#[derive(Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord)]
304+
#[derive(
305+
Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
306+
)]
306307
#[repr(C)]
307308
pub struct CoseKey<'a> {
308309
/// Key type identifier (kty)
@@ -327,7 +328,7 @@ pub struct CoseKey<'a> {
327328
}
328329

329330
#[derive(
330-
Default, Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord,
331+
Default, Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
331332
)]
332333
#[repr(C)]
333334
/// Raw value data structure with associated mask
@@ -336,7 +337,9 @@ pub struct MaskedRawValue {
336337
pub mask: Bytes,
337338
}
338339

339-
#[derive(Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord)]
340+
#[derive(
341+
Debug, Serialize, Deserialize, From, Constructor, PartialEq, Eq, PartialOrd, Ord, Clone,
342+
)]
340343
#[repr(C)]
341344
/// Container for raw values with optional masking
342345
pub struct RawValueType {
@@ -347,7 +350,7 @@ pub struct RawValueType {
347350
/// Type alias for raw value masks
348351
pub type RawValueMaskType = Bytes;
349352

350-
#[derive(Debug, Serialize, Deserialize, From, PartialEq, Eq, PartialOrd, Ord)]
353+
#[derive(Debug, Serialize, Deserialize, From, PartialEq, Eq, PartialOrd, Ord, Clone)]
351354
/// Represents different types of raw values
352355
pub enum RawValueTypeChoice {
353356
TaggedBytes(TaggedBytes),
@@ -356,7 +359,7 @@ pub enum RawValueTypeChoice {
356359

357360
/// Version scheme enumeration as defined in the specification
358361
#[repr(C)]
359-
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord)]
362+
#[derive(Debug, Serialize, Deserialize, From, TryFrom, PartialEq, Eq, PartialOrd, Ord, Clone)]
360363
pub enum VersionScheme {
361364
/// Multi-part numeric version (e.g., 1.2.3)
362365
Multipartnumeric = 1,

0 commit comments

Comments
 (0)