Skip to content

Commit 5ad8bcb

Browse files
committed
Implement multiple variants of Signature tag
* implement de-/serialization logic for signature * add helper functions to write open / close tags * expand signature tests to check different variants * add create functions for `Signature`, refactor code Signed-off-by: Sebastian Ziebell <[email protected]>
1 parent aae675e commit 5ad8bcb

File tree

6 files changed

+416
-100
lines changed

6 files changed

+416
-100
lines changed

cyclonedx-bom/src/models/component.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,7 @@ mod test {
698698
))])),
699699
copyright: Some(CopyrightTexts(vec![Copyright("copyright".to_string())])),
700700
}),
701-
signature: Some(Signature {
702-
algorithm: Algorithm::HS512,
703-
value: "abcdefgh".to_string(),
704-
}),
701+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
705702
}])
706703
.validate_with_context(ValidationContext::default())
707704
.expect("Error while validating");
@@ -790,10 +787,7 @@ mod test {
790787
))])),
791788
copyright: Some(CopyrightTexts(vec![Copyright("copyright".to_string())])),
792789
}),
793-
signature: Some(Signature {
794-
algorithm: Algorithm::HS512,
795-
value: "abcdefgh".to_string(),
796-
}),
790+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
797791
}])
798792
.validate_with_context(ValidationContext::default())
799793
.expect("Error while validating");

cyclonedx-bom/src/models/composition.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ mod test {
145145
aggregate: AggregateType::Complete,
146146
assemblies: Some(vec![BomReference("reference".to_string())]),
147147
dependencies: Some(vec![BomReference("reference".to_string())]),
148-
signature: Some(Signature {
149-
algorithm: Algorithm::HS512,
150-
value: "abcdefgh".to_string(),
151-
}),
148+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
152149
}])
153150
.validate()
154151
.expect("Error while validating");
@@ -162,10 +159,7 @@ mod test {
162159
aggregate: AggregateType::UnknownAggregateType("unknown aggregate type".to_string()),
163160
assemblies: Some(vec![BomReference("reference".to_string())]),
164161
dependencies: Some(vec![BomReference("reference".to_string())]),
165-
signature: Some(Signature {
166-
algorithm: Algorithm::HS512,
167-
value: "abcdefgh".to_string(),
168-
}),
162+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
169163
}])
170164
.validate()
171165
.expect("Error while validating");

cyclonedx-bom/src/models/service.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,7 @@ mod test {
330330
value: NormalizedString::new("value"),
331331
}])),
332332
services: Some(Services(vec![])),
333-
signature: Some(Signature {
334-
algorithm: Algorithm::HS512,
335-
value: "abcdefgh".to_string(),
336-
}),
333+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
337334
}])
338335
.validate_with_context(ValidationContext::default())
339336
.expect("Error while validating");
@@ -393,10 +390,7 @@ mod test {
393390
services: None,
394391
signature: None,
395392
}])),
396-
signature: Some(Signature {
397-
algorithm: Algorithm::HS512,
398-
value: "abcdefgh".to_string(),
399-
}),
393+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
400394
}])
401395
.validate_with_context(ValidationContext::default())
402396
.expect("Error while validating");

cyclonedx-bom/src/models/signature.rs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,15 @@
1818

1919
use std::str::FromStr;
2020

21-
/// Enveloped signature in [JSON Signature Format (JSF)](https://cyberphone.github.io/doc/security/jsf.html)
22-
#[derive(Clone, Debug, PartialEq, Eq)]
23-
pub struct Signature {
24-
/// Signature algorithm.
25-
pub algorithm: Algorithm,
26-
/// The signature data.
27-
pub value: String,
28-
}
29-
30-
/*
3121
/// Enveloped signature in [JSON Signature Format (JSF)](https://cyberphone.github.io/doc/security/jsf.html)
3222
#[derive(Clone, Debug, PartialEq, Eq)]
3323
pub enum Signature {
3424
/// Multiple signatures
3525
Signers(Vec<Signer>),
36-
/// A single signature chain
37-
Chain(Signer),
26+
/// A signature chain consisting of multiple signatures
27+
Chain(Vec<Signer>),
3828
/// A single signature
39-
Signature(Signer),
29+
Single(Signer),
4030
}
4131

4232
/// For now the [`Signer`] struct only holds algorithm and value
@@ -47,10 +37,48 @@ pub struct Signer {
4737
/// The signature data.
4838
pub value: String,
4939
}
50-
*/
40+
41+
impl Signer {
42+
pub fn new(algorithm: Algorithm, value: &str) -> Self {
43+
Self {
44+
algorithm,
45+
value: value.to_string(),
46+
}
47+
}
48+
}
49+
50+
impl Signature {
51+
/// Creates a single signature.
52+
pub fn single(algorithm: Algorithm, value: &str) -> Self {
53+
Self::Single(Signer {
54+
algorithm,
55+
value: value.to_string(),
56+
})
57+
}
58+
59+
/// Creates a chain of multiple signatures
60+
pub fn chain(chain: &[(Algorithm, &str)]) -> Self {
61+
Self::Chain(
62+
chain
63+
.iter()
64+
.map(|(algorithm, value)| Signer::new(*algorithm, value))
65+
.collect(),
66+
)
67+
}
68+
69+
/// Creates a list of multiple signatures.
70+
pub fn signers(signers: &[(Algorithm, &str)]) -> Self {
71+
Self::Signers(
72+
signers
73+
.iter()
74+
.map(|(algorithm, value)| Signer::new(*algorithm, value))
75+
.collect(),
76+
)
77+
}
78+
}
5179

5280
/// Supported signature algorithms.
53-
#[derive(Clone, Debug, PartialEq, Eq)]
81+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5482
pub enum Algorithm {
5583
RS256,
5684
RS384,

0 commit comments

Comments
 (0)