Skip to content

Commit 0769a9b

Browse files
authored
Merge pull request #22 from CycloneDX/spdx-v2.2
Add support for converting to SPDX v2.2
2 parents 5252c9b + 1e1f21c commit 0769a9b

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

cyclonedx/BomFormat.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public enum BomFormat
55
Unsupported,
66
Xml,
77
Json,
8-
SpdxTag
8+
SpdxTag,
9+
SpdxTag_v2_1,
10+
SpdxTag_v2_2
911
}
1012
}

cyclonedx/Commands/Convert/ConvertOutputFormat.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public enum ConvertOutputFormat
55
autodetect,
66
xml,
77
json,
8-
spdxtag
8+
spdxtag,
9+
spdxtag_v2_1,
10+
spdxtag_v2_2
911
}
1012
}

cyclonedx/SpdxTagSerializer.cs

+24-9
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55

66
namespace CycloneDX.CLI
77
{
8+
public enum SpdxVersion
9+
{
10+
v2_1,
11+
v2_2
12+
}
13+
814
public static class SpdxTagSerializer
915
{
1016
public class SpdxSerializationException : Exception
1117
{
1218
public SpdxSerializationException(string message) : base(message) {}
1319
}
1420

15-
public static string Serialize(CycloneDX.Models.v1_2.Bom bom)
21+
public static string Serialize(CycloneDX.Models.v1_2.Bom bom, SpdxVersion version)
1622
{
1723
if (bom.Metadata?.Component?.Name == null || bom.Metadata?.Component?.Version == null)
1824
throw new SpdxSerializationException("For SPDX output top level component name and version are required in the BOM metadata");
@@ -34,7 +40,11 @@ public static string Serialize(CycloneDX.Models.v1_2.Bom bom)
3440

3541
var sb = new StringBuilder();
3642
var componentSb = new StringBuilder();
37-
sb.AppendLine("SPDXVersion: SPDX-2.1");
43+
sb.Append("SPDXVersion: SPDX-");
44+
if (version == SpdxVersion.v2_1)
45+
sb.Append("2.1");
46+
else if (version == SpdxVersion.v2_2)
47+
sb.Append("2.2");
3848
// CC0-1.0 is a requirement when using the SPDX specification
3949
sb.AppendLine("DataLicense: CC0-1.0");
4050
sb.AppendLine($"SPDXID: SPDXRef-DOCUMENT");
@@ -81,6 +91,7 @@ public static string Serialize(CycloneDX.Models.v1_2.Bom bom)
8191
foreach(var hash in component.Hashes)
8292
{
8393
string algStr = null;
94+
8495
switch (hash.Alg)
8596
{
8697
case CycloneDX.Models.v1_2.Hash.HashAlgorithm.SHA_1:
@@ -89,14 +100,18 @@ public static string Serialize(CycloneDX.Models.v1_2.Bom bom)
89100
case CycloneDX.Models.v1_2.Hash.HashAlgorithm.SHA_256:
90101
algStr = "SHA256";
91102
break;
92-
// following algorithms only supported in v2.2
93-
// case Hash.HashAlgorithm.SHA_384:
94-
// algStr = "SHA384";
95-
// break;
96-
// case Hash.HashAlgorithm.SHA_512:
97-
// algStr = "SHA512";
98-
// break;
99103
}
104+
if (version == SpdxVersion.v2_2)
105+
switch (hash.Alg)
106+
{
107+
case CycloneDX.Models.v1_2.Hash.HashAlgorithm.SHA_384:
108+
algStr = "SHA384";
109+
break;
110+
case CycloneDX.Models.v1_2.Hash.HashAlgorithm.SHA_512:
111+
algStr = "SHA512";
112+
break;
113+
}
114+
100115
if (algStr != null)
101116
{
102117
sb.AppendLine($"PackageChecksum: {algStr}: {hash.Content}");

cyclonedx/Utils.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ public static string BomSerializer(CycloneDX.Models.v1_2.Bom bom, BomFormat form
4949
}
5050
else if (format == BomFormat.SpdxTag)
5151
{
52-
return SpdxTagSerializer.Serialize(bom);
52+
return SpdxTagSerializer.Serialize(bom, SpdxVersion.v2_2);
53+
}
54+
else if (format == BomFormat.SpdxTag_v2_1)
55+
{
56+
return SpdxTagSerializer.Serialize(bom, SpdxVersion.v2_1);
57+
}
58+
else if (format == BomFormat.SpdxTag_v2_2)
59+
{
60+
return SpdxTagSerializer.Serialize(bom, SpdxVersion.v2_2);
5361
}
5462
throw new UnsupportedFormatException("Unsupported SBOM file format");
5563
}

0 commit comments

Comments
 (0)