-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
cyclonedx.go
432 lines (369 loc) · 15.9 KB
/
cyclonedx.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
// 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) Niklas Düster. All Rights Reserved.
package cyclonedx
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
)
const (
BOMFormat = "CycloneDX"
defaultVersion = 1
SpecVersion = "1.2"
XMLNamespace = "http://cyclonedx.org/schema/bom/1.2"
)
type AttachedText struct {
Content string `json:"content" xml:",innerxml"`
ContentType string `json:"contentType,omitempty" xml:"content-type,attr,omitempty"`
Encoding string `json:"encoding,omitempty" xml:"encoding,attr,omitempty"`
}
type BOM struct {
// XML specific fields
XMLName xml.Name `json:"-" xml:"bom"`
XMLNS string `json:"-" xml:"xmlns,attr"`
// JSON specific fields
BOMFormat string `json:"bomFormat" xml:"-"`
SpecVersion string `json:"specVersion" xml:"-"`
SerialNumber string `json:"serialNumber,omitempty" xml:"serialNumber,attr,omitempty"`
Version int `json:"version" xml:"version,attr"`
Metadata *Metadata `json:"metadata,omitempty" xml:"metadata,omitempty"`
Components *[]Component `json:"components,omitempty" xml:"components>component,omitempty"`
Services *[]Service `json:"services,omitempty" xml:"services>service,omitempty"`
ExternalReferences *[]ExternalReference `json:"externalReferences,omitempty" xml:"externalReferences>reference,omitempty"`
Dependencies *[]Dependency `json:"dependencies,omitempty" xml:"dependencies>dependency,omitempty"`
}
func NewBOM() *BOM {
return &BOM{
XMLNS: XMLNamespace,
BOMFormat: BOMFormat,
SpecVersion: SpecVersion,
Version: defaultVersion,
}
}
type BOMFileFormat int
const (
BOMFileFormatXML BOMFileFormat = iota
BOMFileFormatJSON
)
// Bool is a convenience function to transform a value of the primitive type bool to a pointer of bool
func Bool(value bool) *bool {
return &value
}
type ComponentType string
const (
ComponentTypeApplication ComponentType = "application"
ComponentTypeContainer ComponentType = "container"
ComponentTypeDevice ComponentType = "device"
ComponentTypeFile ComponentType = "file"
ComponentTypeFirmware ComponentType = "firmware"
ComponentTypeFramework ComponentType = "framework"
ComponentTypeLibrary ComponentType = "library"
ComponentTypeOS ComponentType = "operating-system"
)
type Commit struct {
UID string `json:"uid,omitempty" xml:"uid,omitempty"`
URL string `json:"url,omitempty" xml:"url,omitempty"`
Author *IdentifiableAction `json:"author,omitempty" xml:"author,omitempty"`
Committer *IdentifiableAction `json:"committer,omitempty" xml:"committer,omitempty"`
Message string `json:"message,omitempty" xml:"message,omitempty"`
}
type Component struct {
BOMRef string `json:"bom-ref,omitempty" xml:"bom-ref,attr,omitempty"`
MIMEType string `json:"mime-type,omitempty" xml:"mime-type,attr,omitempty"`
Type ComponentType `json:"type" xml:"type,attr"`
Supplier *OrganizationalEntity `json:"supplier,omitempty" xml:"supplier,omitempty"`
Author string `json:"author,omitempty" xml:"author,omitempty"`
Publisher string `json:"publisher,omitempty" xml:"publisher,omitempty"`
Group string `json:"group,omitempty" xml:"group,omitempty"`
Name string `json:"name" xml:"name"`
Version string `json:"version" xml:"version"`
Description string `json:"description,omitempty" xml:"description,omitempty"`
Scope Scope `json:"scope,omitempty" xml:"scope,omitempty"`
Hashes *[]Hash `json:"hashes,omitempty" xml:"hashes>hash,omitempty"`
Licenses *Licenses `json:"licenses,omitempty" xml:"licenses,omitempty"`
Copyright string `json:"copyright,omitempty" xml:"copyright,omitempty"`
CPE string `json:"cpe,omitempty" xml:"cpe,omitempty"`
PackageURL string `json:"purl,omitempty" xml:"purl,omitempty"`
SWID *SWID `json:"swid,omitempty" xml:"swid,omitempty"`
Modified *bool `json:"modified,omitempty" xml:"modified,omitempty"`
Pedigree *Pedigree `json:"pedigree,omitempty" xml:"pedigree,omitempty"`
ExternalReferences *[]ExternalReference `json:"externalReferences,omitempty" xml:"externalReferences>reference,omitempty"`
Components *[]Component `json:"components,omitempty" xml:"components>component,omitempty"`
}
type DataClassification struct {
Flow DataFlow `json:"flow" xml:"flow,attr"`
Classification string `json:"classification" xml:",innerxml"`
}
type DataFlow string
const (
DataFlowBidirectional DataFlow = "bi-directional"
DataFlowInbound DataFlow = "inbound"
DataFlowOutbound DataFlow = "outbound"
DataFlowUnknown DataFlow = "unknown"
)
type Dependency struct {
Ref string `xml:"ref,attr"`
Dependencies *[]Dependency `xml:"dependency,omitempty"`
}
// dependencyJSON is temporarily used for marshalling and unmarshalling Dependency instances to and from JSON
type dependencyJSON struct {
Ref string `json:"ref"`
DependsOn []string `json:"dependsOn,omitempty"`
}
func (d Dependency) MarshalJSON() ([]byte, error) {
if d.Dependencies == nil || len(*d.Dependencies) == 0 {
return json.Marshal(&dependencyJSON{
Ref: d.Ref,
})
}
dependencyRefs := make([]string, len(*d.Dependencies))
for i, dependency := range *d.Dependencies {
dependencyRefs[i] = dependency.Ref
}
return json.Marshal(&dependencyJSON{
Ref: d.Ref,
DependsOn: dependencyRefs,
})
}
func (d *Dependency) UnmarshalJSON(bytes []byte) error {
dependency := new(dependencyJSON)
if err := json.Unmarshal(bytes, dependency); err != nil {
return err
}
d.Ref = dependency.Ref
if len(dependency.DependsOn) == 0 {
return nil
}
dependencies := make([]Dependency, len(dependency.DependsOn))
for i, dep := range dependency.DependsOn {
dependencies[i] = Dependency{
Ref: dep,
}
}
d.Dependencies = &dependencies
return nil
}
type Diff struct {
Text *AttachedText `json:"text,omitempty" xml:"text,omitempty"`
URL string `json:"url,omitempty" xml:"url,omitempty"`
}
type ExternalReference struct {
URL string `json:"url" xml:"url"`
Comment string `json:"comment,omitempty" xml:"comment,omitempty"`
Type ExternalReferenceType `json:"type" xml:"type,attr"`
}
type ExternalReferenceType string
const (
ERTypeAdvisories ExternalReferenceType = "advisories"
ERTypeBOM ExternalReferenceType = "bom"
ERTypeBuildMeta ExternalReferenceType = "build-meta"
ERTypeBuildSystem ExternalReferenceType = "build-system"
ERTypeChat ExternalReferenceType = "chat"
ERTypeDistribution ExternalReferenceType = "distribution"
ERTypeDocumentation ExternalReferenceType = "documentation"
ERTypeLicense ExternalReferenceType = "license"
ERTypeMailingList ExternalReferenceType = "mailing-list"
ERTypeOther ExternalReferenceType = "other"
ERTypeIssueTracker ExternalReferenceType = "issue-tracker"
ERTypeSocial ExternalReferenceType = "social"
ERTypeSupport ExternalReferenceType = "support"
ERTypeVCS ExternalReferenceType = "vcs"
ERTypeWebsite ExternalReferenceType = "website"
)
type Hash struct {
Algorithm HashAlgorithm `json:"alg" xml:"alg,attr"`
Value string `json:"content" xml:",innerxml"`
}
type HashAlgorithm string
const (
HashAlgoMD5 HashAlgorithm = "MD5"
HashAlgoSHA1 HashAlgorithm = "SHA-1"
HashAlgoSHA256 HashAlgorithm = "SHA-256"
HashAlgoSHA384 HashAlgorithm = "SHA-384"
HashAlgoSHA512 HashAlgorithm = "SHA-512"
HashAlgoSHA3_256 HashAlgorithm = "SHA3-256"
HashAlgoSHA3_512 HashAlgorithm = "SHA3-512"
HashAlgoBlake2b_256 HashAlgorithm = "BLAKE2b-256"
HashAlgoBlake2b_384 HashAlgorithm = "BLAKE2b-384"
HashAlgoBlake2b_512 HashAlgorithm = "BLAKE2b-512"
HashAlgoBlake3 HashAlgorithm = "BLAKE3"
)
type IdentifiableAction struct {
Timestamp string `json:"timestamp,omitempty" xml:"timestamp,omitempty"`
Name string `json:"name,omitempty" xml:"name,omitempty"`
EMail string `json:"email,omitempty" xml:"email,omitempty"`
}
type Issue struct {
ID string `json:"id" xml:"id"`
Name string `json:"name" xml:"name"`
Description string `json:"description" xml:"description"`
Source *Source `json:"source,omitempty" xml:"source,omitempty"`
References *[]string `json:"references,omitempty" xml:"references>url,omitempty"`
Type IssueType `json:"type" xml:"type,attr"`
}
type IssueType string
const (
IssueTypeDefect IssueType = "defect"
IssueTypeEnhancement IssueType = "enhancement"
IssueTypeSecurity IssueType = "security"
)
type License struct {
ID string `json:"id,omitempty" xml:"id,omitempty"`
Name string `json:"name,omitempty" xml:"name,omitempty"`
Text *AttachedText `json:"text,omitempty" xml:"text,omitempty"`
URL string `json:"url,omitempty" xml:"url,omitempty"`
}
type Licenses []LicenseChoice
func (l Licenses) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(l) == 0 {
return nil
}
if err := e.EncodeToken(start); err != nil {
return err
}
for _, choice := range l {
if choice.License != nil && choice.Expression != "" {
return fmt.Errorf("either license or expression must be set, but not both")
}
if choice.License != nil {
if err := e.EncodeElement(choice.License, xml.StartElement{Name: xml.Name{Local: "license"}}); err != nil {
return err
}
} else if choice.Expression != "" {
if err := e.EncodeElement(choice.Expression, xml.StartElement{Name: xml.Name{Local: "expression"}}); err != nil {
return err
}
}
}
return e.EncodeToken(start.End())
}
func (l *Licenses) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
licenses := make([]LicenseChoice, 0)
for {
token, err := d.Token()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
switch tokenType := token.(type) {
case xml.StartElement:
if tokenType.Name.Local == "expression" {
var expression string
if err = d.DecodeElement(&expression, &tokenType); err != nil {
return err
}
licenses = append(licenses, LicenseChoice{Expression: expression})
} else if tokenType.Name.Local == "license" {
var license License
if err = d.DecodeElement(&license, &tokenType); err != nil {
return err
}
licenses = append(licenses, LicenseChoice{License: &license})
} else {
return fmt.Errorf("unknown element: %s", tokenType.Name.Local)
}
}
}
*l = licenses
return nil
}
type LicenseChoice struct {
License *License `json:"license,omitempty" xml:"-"`
Expression string `json:"expression,omitempty" xml:"-"`
}
type Metadata struct {
Timestamp string `json:"timestamp,omitempty" xml:"timestamp,omitempty"`
Tools *[]Tool `json:"tools,omitempty" xml:"tools>tool,omitempty"`
Authors *[]OrganizationalContact `json:"authors,omitempty" xml:"authors>author,omitempty"`
Component *Component `json:"component,omitempty" xml:"component,omitempty"`
Manufacture *OrganizationalEntity `json:"manufacture,omitempty" xml:"manufacture,omitempty"`
Supplier *OrganizationalEntity `json:"supplier,omitempty" xml:"supplier,omitempty"`
}
type OrganizationalContact struct {
Name string `json:"name,omitempty" xml:"name,omitempty"`
EMail string `json:"email,omitempty" xml:"email,omitempty"`
Phone string `json:"phone,omitempty" xml:"phone,omitempty"`
}
type OrganizationalEntity struct {
Name string `json:"name" xml:"name"`
URL *[]string `json:"url,omitempty" xml:"url,omitempty"`
Contact *[]OrganizationalContact `json:"contact,omitempty" xml:"contact,omitempty"`
}
type Patch struct {
Diff *Diff `json:"diff,omitempty" xml:"diff,omitempty"`
Resolves *[]Issue `json:"resolves,omitempty" xml:"resolves>issue,omitempty"`
Type PatchType `json:"type" xml:"type,attr"`
}
type PatchType string
const (
PatchTypeBackport PatchType = "backport"
PatchTypeCherryPick PatchType = "cherry-pick"
PatchTypeMonkey PatchType = "monkey"
PatchTypeUnofficial PatchType = "unofficial"
)
type Pedigree struct {
Ancestors *[]Component `json:"ancestors,omitempty" xml:"ancestors>component,omitempty"`
Descendants *[]Component `json:"descendants,omitempty" xml:"descendants>component,omitempty"`
Variants *[]Component `json:"variants,omitempty" xml:"variants>component,omitempty"`
Commits *[]Commit `json:"commits,omitempty" xml:"commits>commit,omitempty"`
Patches *[]Patch `json:"patches,omitempty" xml:"patches>patch,omitempty"`
Notes string `json:"notes,omitempty" xml:"notes,omitempty"`
}
type Scope string
const (
ScopeExcluded Scope = "excluded"
ScopeOptional Scope = "optional"
ScopeRequired Scope = "required"
)
type Service struct {
BOMRef string `json:"bom-ref,omitempty" xml:"bom-ref,attr,omitempty"`
Provider *OrganizationalEntity `json:"provider,omitempty" xml:"provider,omitempty"`
Group string `json:"group,omitempty" xml:"group,omitempty"`
Name string `json:"name" xml:"name"`
Version string `json:"version,omitempty" xml:"version,omitempty"`
Description string `json:"description,omitempty" xml:"description,omitempty"`
Endpoints *[]string `json:"endpoints,omitempty" xml:"endpoints>endpoint,omitempty"`
Authenticated *bool `json:"authenticated,omitempty" xml:"authenticated,omitempty"`
CrossesTrustBoundary *bool `json:"x-trust-boundary,omitempty" xml:"x-trust-boundary,omitempty"`
Data *[]DataClassification `json:"data,omitempty" xml:"data>classification,omitempty"`
Licenses *Licenses `json:"licenses,omitempty" xml:"licenses,omitempty"`
ExternalReferences *[]ExternalReference `json:"externalReferences,omitempty" xml:"externalReferences>reference,omitempty"`
Services *[]Service `json:"services,omitempty" xml:"services>service,omitempty"`
}
type Source struct {
Name string `json:"name,omitempty" xml:"name,omitempty"`
URL string `json:"url,omitempty" xml:"url,omitempty"`
}
type SWID struct {
Text *AttachedText `json:"text,omitempty" xml:"text,omitempty"`
URL string `json:"url,omitempty" xml:"url,attr,omitempty"`
TagID string `json:"tagId" xml:"tagId,attr"`
Name string `json:"name" xml:"name,attr"`
Version string `json:"version,omitempty" xml:"version,attr,omitempty"`
TagVersion *int `json:"tagVersion,omitempty" xml:"tagVersion,attr,omitempty"`
Patch *bool `json:"patch,omitempty" xml:"patch,attr,omitempty"`
}
type Tool struct {
Vendor string `json:"vendor,omitempty" xml:"vendor,omitempty"`
Name string `json:"name" xml:"name"`
Version string `json:"version,omitempty" xml:"version,omitempty"`
Hashes *[]Hash `json:"hashes,omitempty" xml:"hashes>hash,omitempty"`
}