-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
convert.go
691 lines (588 loc) · 16.6 KB
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// This file is part of CycloneDX Go
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) OWASP Foundation. All Rights Reserved.
package cyclonedx
import "fmt"
// copyAndConvert returns a converted copy of the BOM, adhering to a given SpecVersion.
func (b BOM) copyAndConvert(specVersion SpecVersion) (*BOM, error) {
var bomCopy BOM
err := b.copy(&bomCopy)
if err != nil {
return nil, fmt.Errorf("failed to copy bom: %w", err)
}
bomCopy.convert(specVersion)
return &bomCopy, nil
}
// convert modifies the BOM such that it adheres to a given SpecVersion.
func (b *BOM) convert(specVersion SpecVersion) {
if specVersion < SpecVersion1_1 {
b.SerialNumber = ""
b.ExternalReferences = nil
}
if specVersion < SpecVersion1_2 {
b.Dependencies = nil
b.Metadata = nil
b.Services = nil
}
if specVersion < SpecVersion1_3 {
b.Compositions = nil
}
if specVersion < SpecVersion1_4 {
b.Vulnerabilities = nil
}
if specVersion < SpecVersion1_5 {
b.Annotations = nil
b.Formulation = nil
}
if specVersion < SpecVersion1_6 {
b.Declarations = nil
b.Definitions = nil
}
if b.Metadata != nil {
if specVersion < SpecVersion1_3 {
b.Metadata.Licenses = nil
b.Metadata.Properties = nil
}
if specVersion < SpecVersion1_5 {
b.Metadata.Lifecycles = nil
}
if specVersion < SpecVersion1_6 {
b.Metadata.Manufacturer = nil
}
recurseComponent(b.Metadata.Component, componentConverter(specVersion))
convertLicenses(b.Metadata.Licenses, specVersion)
convertTools(b.Metadata.Tools, specVersion)
convertOrganizationalEntity(b.Metadata.Manufacture, specVersion)
convertOrganizationalEntity(b.Metadata.Supplier, specVersion)
if b.Metadata.Authors != nil {
for i := range *b.Metadata.Authors {
convertOrganizationalContact(&(*b.Metadata.Authors)[i], specVersion)
}
}
}
if b.Components != nil {
for i := range *b.Components {
recurseComponent(&(*b.Components)[i], componentConverter(specVersion))
}
}
if b.Services != nil {
for i := range *b.Services {
recurseService(&(*b.Services)[i], serviceConverter(specVersion))
}
}
if b.Vulnerabilities != nil {
convertVulnerabilities(b.Vulnerabilities, specVersion)
}
if b.Compositions != nil {
convertCompositions(b.Compositions, specVersion)
}
if b.ExternalReferences != nil {
convertExternalReferences(b.ExternalReferences, specVersion)
}
if b.Annotations != nil {
convertAnnotations(b.Annotations, specVersion)
}
b.SpecVersion = specVersion
b.XMLNS = xmlNamespaces[specVersion]
b.JSONSchema = jsonSchemas[specVersion]
}
// componentConverter modifies a Component such that it adheres to a given SpecVersion.
func componentConverter(specVersion SpecVersion) func(*Component) {
return func(c *Component) {
if specVersion < SpecVersion1_1 {
c.BOMRef = ""
c.ExternalReferences = nil
if c.Modified == nil {
c.Modified = Bool(false)
}
c.Pedigree = nil
}
if specVersion < SpecVersion1_2 {
c.Author = ""
c.MIMEType = ""
if c.Pedigree != nil {
c.Pedigree.Patches = nil
}
c.Supplier = nil
c.SWID = nil
}
if specVersion < SpecVersion1_3 {
c.Properties = nil
}
if specVersion < SpecVersion1_4 {
c.ReleaseNotes = nil
if c.Version == "" {
c.Version = "0.0.0"
}
}
if specVersion < SpecVersion1_5 {
c.ModelCard = nil
c.Data = nil
}
if specVersion < SpecVersion1_6 {
c.SWHID = nil
c.OmniborID = nil
c.Manufacturer = nil
c.Authors = nil
}
if !specVersion.supportsComponentType(c.Type) {
c.Type = ComponentTypeApplication
}
convertExternalReferences(c.ExternalReferences, specVersion)
convertHashes(c.Hashes, specVersion)
convertLicenses(c.Licenses, specVersion)
convertEvidence(c, specVersion)
convertModelCard(c, specVersion)
if !specVersion.supportsScope(c.Scope) {
c.Scope = ""
}
}
}
func convertEvidence(c *Component, specVersion SpecVersion) {
if c.Evidence == nil {
return
}
if specVersion < SpecVersion1_3 {
c.Evidence = nil
return
}
if specVersion < SpecVersion1_5 {
c.Evidence.Identity = nil
c.Evidence.Occurrences = nil
c.Evidence.Callstack = nil
return
}
if specVersion < SpecVersion1_6 {
// Spec version 1.5 uses only one Identity.
// cf. https://cyclonedx.org/docs/1.5/json/#components_items_evidence_identity
if c.Evidence.Identity != nil {
ids := *c.Evidence.Identity
ids = ids[:1]
c.Evidence.Identity = &ids
}
if c.Evidence.Occurrences != nil {
for i := range *c.Evidence.Occurrences {
occ := &(*c.Evidence.Occurrences)[i]
occ.Line = nil
occ.Offset = nil
occ.Symbol = ""
occ.AdditionalContext = ""
}
}
}
convertLicenses(c.Evidence.Licenses, specVersion)
}
func convertCompositions(comps *[]Composition, specVersion SpecVersion) {
if comps == nil {
return
}
for i := range *comps {
comp := &(*comps)[i]
if !specVersion.supportsCompositionAggregate(comp.Aggregate) {
comp.Aggregate = CompositionAggregateUnknown
}
}
}
// convertExternalReferences modifies an ExternalReference slice such that it adheres to a given SpecVersion.
func convertExternalReferences(extRefs *[]ExternalReference, specVersion SpecVersion) {
if extRefs == nil {
return
}
for i := range *extRefs {
extRef := &(*extRefs)[i]
if !specVersion.supportsExternalReferenceType(extRef.Type) {
extRef.Type = ERTypeOther
}
if specVersion < SpecVersion1_3 {
extRef.Hashes = nil
}
}
}
// convertHashes modifies a Hash slice such that it adheres to a given SpecVersion.
// If after the conversion no valid hashes are left in the slice, it will be nilled.
func convertHashes(hashes *[]Hash, specVersion SpecVersion) {
if hashes == nil {
return
}
converted := make([]Hash, 0)
for i := range *hashes {
hash := (*hashes)[i]
if specVersion.supportsHashAlgorithm(hash.Algorithm) {
converted = append(converted, hash)
}
}
if len(converted) == 0 {
*hashes = nil
} else {
*hashes = converted
}
}
// convertLicenses modifies a Licenses slice such that it adheres to a given SpecVersion.
// If after the conversion no valid licenses are left in the slice, it will be nilled.
func convertLicenses(licenses *Licenses, specVersion SpecVersion) {
if licenses == nil {
return
}
if specVersion < SpecVersion1_1 {
converted := make(Licenses, 0)
for i := range *licenses {
choice := &(*licenses)[i]
if choice.License != nil {
if choice.License.ID == "" && choice.License.Name == "" {
choice.License = nil
} else {
choice.License.Text = nil
choice.License.URL = ""
}
}
choice.Expression = ""
if choice.License != nil {
converted = append(converted, *choice)
}
}
if len(converted) == 0 {
*licenses = nil
} else {
*licenses = converted
}
}
if specVersion < SpecVersion1_5 {
for i := range *licenses {
choice := &(*licenses)[i]
if choice.License != nil {
choice.License.BOMRef = ""
choice.License.Licensing = nil
choice.License.Properties = nil
}
}
}
if specVersion < SpecVersion1_6 {
for i := range *licenses {
choice := &(*licenses)[i]
if choice.License == nil {
continue
}
choice.License.Acknowledgement = ""
if choice.License.Licensing == nil {
continue
}
if choice.License.Licensing.Licensor != nil {
convertOrganizationalEntity(choice.License.Licensing.Licensor.Organization, specVersion)
}
if choice.License.Licensing.Licensee != nil {
convertOrganizationalEntity(choice.License.Licensing.Licensee.Organization, specVersion)
}
if choice.License.Licensing.Purchaser != nil {
convertOrganizationalEntity(choice.License.Licensing.Purchaser.Organization, specVersion)
}
}
}
}
func convertOrganizationalEntity(org *OrganizationalEntity, specVersion SpecVersion) {
if org == nil {
return
}
if specVersion < SpecVersion1_5 {
org.BOMRef = ""
if org.Contact != nil {
for i := range *org.Contact {
convertOrganizationalContact(&(*org.Contact)[i], specVersion)
}
}
}
if specVersion < SpecVersion1_6 {
org.Address = nil
}
}
func convertOrganizationalContact(c *OrganizationalContact, specVersion SpecVersion) {
if c == nil {
return
}
if specVersion < SpecVersion1_5 {
c.BOMRef = ""
}
}
func convertModelCard(c *Component, specVersion SpecVersion) {
if c.ModelCard == nil {
return
}
if specVersion < SpecVersion1_6 {
if c.ModelCard.Considerations != nil {
c.ModelCard.Considerations.EnvironmentalConsiderations = nil
}
}
}
func convertVulnerabilities(vulns *[]Vulnerability, specVersion SpecVersion) {
if vulns == nil {
return
}
for i := range *vulns {
vuln := &(*vulns)[i]
convertTools(vuln.Tools, specVersion)
if specVersion < SpecVersion1_5 {
vuln.ProofOfConcept = nil
vuln.Rejected = ""
vuln.Workaround = ""
}
if specVersion < SpecVersion1_6 {
if vuln.Credits != nil {
if vuln.Credits.Organizations != nil {
for i := range *vuln.Credits.Organizations {
convertOrganizationalEntity(&(*vuln.Credits.Organizations)[i], specVersion)
}
}
if vuln.Credits.Individuals != nil {
for i := range *vuln.Credits.Individuals {
convertOrganizationalContact(&(*vuln.Credits.Individuals)[i], specVersion)
}
}
}
}
if vuln.Ratings != nil {
for j := range *vuln.Ratings {
rating := &(*vuln.Ratings)[j]
if !specVersion.supportsScoringMethod(rating.Method) {
rating.Method = ScoringMethodOther
}
}
}
}
}
func convertAnnotations(annotations *[]Annotation, specVersion SpecVersion) {
if annotations == nil {
return
}
if specVersion < SpecVersion1_6 {
for i := range *annotations {
ann := (*annotations)[i]
if ann.Annotator == nil {
continue
}
convertOrganizationalEntity(ann.Annotator.Organization, specVersion)
recurseService(ann.Annotator.Service, serviceConverter(specVersion))
}
}
}
// serviceConverter modifies a Service such that it adheres to a given SpecVersion.
func serviceConverter(specVersion SpecVersion) func(*Service) {
return func(s *Service) {
if specVersion < SpecVersion1_3 {
s.Properties = nil
}
if specVersion < SpecVersion1_4 {
s.ReleaseNotes = nil
}
convertOrganizationalEntity(s.Provider, specVersion)
convertExternalReferences(s.ExternalReferences, specVersion)
}
}
// convertTools modifies a ToolsChoice such that it adheres to a given SpecVersion.
func convertTools(tools *ToolsChoice, specVersion SpecVersion) {
if tools == nil {
return
}
if specVersion < SpecVersion1_5 {
convertedTools := make([]Tool, 0)
if tools.Components != nil {
for i := range *tools.Components {
tool := convertComponentToTool((*tools.Components)[i], specVersion)
if tool != nil {
convertedTools = append(convertedTools, *tool)
}
}
tools.Components = nil
}
if tools.Services != nil {
for i := range *tools.Services {
tool := convertServiceToTool((*tools.Services)[i], specVersion)
if tool != nil {
convertedTools = append(convertedTools, *tool)
}
}
tools.Services = nil
}
if len(convertedTools) > 0 {
if tools.Tools == nil {
tools.Tools = &convertedTools
} else {
*tools.Tools = append(*tools.Tools, convertedTools...)
}
}
}
if tools.Services != nil {
for i := range *tools.Services {
convertOrganizationalEntity((*tools.Services)[i].Provider, specVersion)
}
}
if tools.Tools != nil {
for i := range *tools.Tools {
convertTool(&(*tools.Tools)[i], specVersion)
}
}
}
// convertTool modifies a Tool such that it adheres to a given SpecVersion.
func convertTool(tool *Tool, specVersion SpecVersion) {
if tool == nil {
return
}
if specVersion < SpecVersion1_4 {
tool.ExternalReferences = nil
}
convertExternalReferences(tool.ExternalReferences, specVersion)
convertHashes(tool.Hashes, specVersion)
}
// convertComponentToTool converts a Component to a Tool for use in ToolsChoice.Tools.
func convertComponentToTool(component Component, _ SpecVersion) *Tool {
tool := Tool{
Vendor: component.Author,
Name: component.Name,
Version: component.Version,
Hashes: component.Hashes,
ExternalReferences: component.ExternalReferences,
}
if component.Supplier != nil {
// There is no perfect 1:1 mapping for the Vendor field, but Supplier comes closest.
// https://github.com/CycloneDX/cyclonedx-go/issues/115#issuecomment-1688710539
tool.Vendor = component.Supplier.Name
}
return &tool
}
// convertServiceToTool converts a Service to a Tool for use in ToolsChoice.Tools.
func convertServiceToTool(service Service, _ SpecVersion) *Tool {
tool := Tool{
Name: service.Name,
Version: service.Version,
ExternalReferences: service.ExternalReferences,
}
if service.Provider != nil {
tool.Vendor = service.Provider.Name
}
return &tool
}
func recurseComponent(component *Component, f func(c *Component)) {
if component == nil {
return
}
f(component)
if component.Components != nil {
for i := range *component.Components {
recurseComponent(&(*component.Components)[i], f)
}
}
if component.Pedigree != nil {
if component.Pedigree.Ancestors != nil {
for i := range *component.Pedigree.Ancestors {
recurseComponent(&(*component.Pedigree.Ancestors)[i], f)
}
}
if component.Pedigree.Descendants != nil {
for i := range *component.Pedigree.Descendants {
recurseComponent(&(*component.Pedigree.Descendants)[i], f)
}
}
if component.Pedigree.Variants != nil {
for i := range *component.Pedigree.Variants {
recurseComponent(&(*component.Pedigree.Variants)[i], f)
}
}
}
}
func recurseService(service *Service, f func(s *Service)) {
if service == nil {
return
}
f(service)
if service.Services != nil {
for i := range *service.Services {
recurseService(&(*service.Services)[i], f)
}
}
}
func (sv SpecVersion) supportsComponentType(cType ComponentType) bool {
switch cType {
case ComponentTypeApplication, ComponentTypeDevice, ComponentTypeFramework, ComponentTypeLibrary, ComponentTypeOS:
return sv >= SpecVersion1_0
case ComponentTypeFile:
return sv >= SpecVersion1_1
case ComponentTypeContainer, ComponentTypeFirmware:
return sv >= SpecVersion1_2
case ComponentTypeData, ComponentTypeDeviceDriver, ComponentTypeMachineLearningModel, ComponentTypePlatform:
return sv >= SpecVersion1_5
}
return false
}
func (sv SpecVersion) supportsCompositionAggregate(ca CompositionAggregate) bool {
switch ca {
case CompositionAggregateIncompleteFirstPartyOpenSourceOnly, CompositionAggregateIncompleteFirstPartyProprietaryOnly,
CompositionAggregateIncompleteThirdPartyOpenSourceOnly, CompositionAggregateIncompleteThirdPartyProprietaryOnly:
return sv >= SpecVersion1_5
}
return sv >= SpecVersion1_3
}
func (sv SpecVersion) supportsExternalReferenceType(ert ExternalReferenceType) bool {
switch ert {
case ERTypeAdversaryModel,
ERTypeAttestation,
ERTypeCertificationReport,
ERTypeCodifiedInfrastructure,
ERTypeComponentAnalysisReport,
ERTypeConfiguration,
ERTypeDistributionIntake,
ERTypeDynamicAnalysisReport,
ERTypeEvidence,
ERTypeExploitabilityStatement,
ERTypeFormulation,
ERTypeLog,
ERTypeMaturityReport,
ERTypeModelCard,
ERTypePentestReport,
ERTypeQualityMetrics,
ERTypeRiskAssessment,
ERTypeRuntimeAnalysisReport,
ERTypeStaticAnalysisReport,
ERTypeThreatModel,
ERTypeVulnerabilityAssertion:
return sv >= SpecVersion1_5
}
return sv >= SpecVersion1_1
}
func (sv SpecVersion) supportsHashAlgorithm(algo HashAlgorithm) bool {
switch algo {
case HashAlgoMD5, HashAlgoSHA1, HashAlgoSHA256, HashAlgoSHA384, HashAlgoSHA512, HashAlgoSHA3_256, HashAlgoSHA3_512:
return sv >= SpecVersion1_0
case HashAlgoSHA3_384, HashAlgoBlake2b_256, HashAlgoBlake2b_384, HashAlgoBlake2b_512, HashAlgoBlake3:
return sv >= SpecVersion1_2
}
return false
}
func (sv SpecVersion) supportsScope(scope Scope) bool {
switch scope {
case ScopeRequired, ScopeOptional:
return sv >= SpecVersion1_0
case ScopeExcluded:
return sv >= SpecVersion1_2
}
return false
}
func (sv SpecVersion) supportsScoringMethod(method ScoringMethod) bool {
switch method {
case ScoringMethodCVSSv2, ScoringMethodCVSSv3, ScoringMethodCVSSv31, ScoringMethodOWASP, ScoringMethodOther:
return sv >= SpecVersion1_4
case ScoringMethodCVSSv4, ScoringMethodSSVC:
return sv >= SpecVersion1_5
}
return false
}