| 1 |
package uswid |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"compress/zlib" |
| 6 |
"encoding/binary" |
| 7 |
"encoding/json" |
| 8 |
"encoding/xml" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"strings" |
| 13 |
"io/ioutil" |
| 14 |
|
| 15 |
"github.com/fxamacker/cbor/v2" |
| 16 |
"github.com/CodingVoid/swid" |
| 17 |
"github.com/google/uuid" |
| 18 |
) |
| 19 |
|
| 20 |
var magic []byte = []byte{0x53, 0x42, 0x4F, 0x4D, 0xD6, 0xBA, 0x2E, 0xAC, 0xA3, 0xE6, 0x7A, 0x52, 0xAA, 0xEE, 0x3B, 0xAF} |
| 21 |
const flagCompressZlib = 0x01 |
| 22 |
|
| 23 |
|
| 24 |
type UswidSoftwareIdentity struct { |
| 25 |
Identities []swid.SoftwareIdentity |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
func jsonMinify(json string, stripSpace bool) string { |
| 30 |
var out strings.Builder |
| 31 |
indexMultilineComment := -1 |
| 32 |
indexInlineComment := -1 |
| 33 |
inJsonStr := false |
| 34 |
skip := false |
| 35 |
for i, r := range json { |
| 36 |
if skip { |
| 37 |
skip = false |
| 38 |
continue |
| 39 |
} |
| 40 |
switch r { |
| 41 |
case '/': |
| 42 |
if !inJsonStr && indexMultilineComment == -1 && indexInlineComment == -1 { |
| 43 |
|
| 44 |
|
| 45 |
if len(json) > i+1 { |
| 46 |
if json[i+1] == '/' { |
| 47 |
indexInlineComment = i |
| 48 |
|
| 49 |
|
| 50 |
skip = true |
| 51 |
} else if json[i+1] == '*' { |
| 52 |
indexMultilineComment = i |
| 53 |
|
| 54 |
|
| 55 |
skip = true |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
case '*': |
| 60 |
if indexMultilineComment != -1 { |
| 61 |
|
| 62 |
|
| 63 |
if len(json) > i+1 { |
| 64 |
if json[i+1] == '/' { |
| 65 |
indexMultilineComment = -1 |
| 66 |
|
| 67 |
|
| 68 |
skip = true |
| 69 |
|
| 70 |
|
| 71 |
continue |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
case '"': |
| 76 |
if inJsonStr { |
| 77 |
inJsonStr = false |
| 78 |
} else if !inJsonStr && indexMultilineComment == -1 && indexInlineComment == -1 { |
| 79 |
|
| 80 |
|
| 81 |
inJsonStr = true |
| 82 |
} |
| 83 |
case '\\': |
| 84 |
if len(json) > i+1 { |
| 85 |
if json[i+1] == '"' { |
| 86 |
|
| 87 |
|
| 88 |
out.WriteRune('\\') |
| 89 |
out.WriteRune('"') |
| 90 |
skip = true |
| 91 |
continue |
| 92 |
} else if json[i+1] == '\\' { |
| 93 |
|
| 94 |
|
| 95 |
out.WriteRune('\\') |
| 96 |
skip = true |
| 97 |
} |
| 98 |
} |
| 99 |
case '\n': |
| 100 |
indexInlineComment = -1 |
| 101 |
case '\r': |
| 102 |
indexInlineComment = -1 |
| 103 |
} |
| 104 |
if indexMultilineComment == -1 && indexInlineComment == -1 { |
| 105 |
|
| 106 |
if !stripSpace || inJsonStr || (r != '\n' && r != '\r' && r != '\t' && r != ' ') { |
| 107 |
|
| 108 |
|
| 109 |
out.WriteRune(r) |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
return out.String() |
| 114 |
} |
| 115 |
|
| 116 |
func (uswid *UswidSoftwareIdentity) FromFile(filepath string) error { |
| 117 |
inputFile, err := ioutil.ReadFile(filepath) |
| 118 |
if err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
ifParts := strings.Split(filepath, ".") |
| 124 |
switch ifParts[len(ifParts)-1] { |
| 125 |
case "pc": |
| 126 |
pcStr := strings.ReplaceAll(string(inputFile), "\r\n", "\n") |
| 127 |
err = uswid.FromPC(pcStr, filepath) |
| 128 |
case "json": |
| 129 |
jsonStr := jsonMinify(string(inputFile), false) |
| 130 |
err = uswid.FromJSON(jsonStr) |
| 131 |
case "xml": |
| 132 |
err = uswid.FromXML(string(inputFile)) |
| 133 |
case "cbor": |
| 134 |
err = uswid.FromCBOR(inputFile, false) |
| 135 |
case "uswid": |
| 136 |
fallthrough |
| 137 |
default: |
| 138 |
_, err = uswid.FromUSWID(inputFile) |
| 139 |
} |
| 140 |
if err != nil { |
| 141 |
return fmt.Errorf("parsing %s: %w", filepath, err) |
| 142 |
} |
| 143 |
return nil |
| 144 |
} |
| 145 |
|
| 146 |
func (uswid *UswidSoftwareIdentity) FromCBOR(blob []byte, compressed bool) error { |
| 147 |
buf := bytes.NewBuffer(blob) |
| 148 |
var decoder *cbor.Decoder |
| 149 |
if compressed { |
| 150 |
rd, err := zlib.NewReader(buf) |
| 151 |
if err != nil { |
| 152 |
return fmt.Errorf("create zlib reader: %w", err) |
| 153 |
} |
| 154 |
decoder = cbor.NewDecoder(rd) |
| 155 |
} else { |
| 156 |
decoder = cbor.NewDecoder(buf) |
| 157 |
} |
| 158 |
for { |
| 159 |
var id swid.SoftwareIdentity |
| 160 |
err := decoder.Decode(&id) |
| 161 |
if err == io.EOF { |
| 162 |
break |
| 163 |
} |
| 164 |
if err != nil { |
| 165 |
return fmt.Errorf("decoding cbor: %w", err) |
| 166 |
} |
| 167 |
uswid.Identities = append(uswid.Identities, id) |
| 168 |
} |
| 169 |
return nil |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
func (uswid *UswidSoftwareIdentity) FromUSWID(blob []byte) (int, error) { |
| 174 |
var err error |
| 175 |
offset := bytes.Index(blob, magic) |
| 176 |
if offset == -1 { |
| 177 |
return -1, errors.New("could not find uswid data") |
| 178 |
} |
| 179 |
headerVersion := blob[offset+16] |
| 180 |
if headerVersion != 2 { |
| 181 |
return offset, errors.New("no known header version") |
| 182 |
} |
| 183 |
|
| 184 |
payloadLen := binary.LittleEndian.Uint32(blob[offset+19 : offset+23]) |
| 185 |
flags := blob[offset+23] |
| 186 |
if (flags & flagCompressZlib) != 0 { |
| 187 |
err = uswid.FromCBOR(blob[offset+24:uint32(offset)+24+payloadLen], true) |
| 188 |
} else { |
| 189 |
err = uswid.FromCBOR(blob[offset+24:uint32(offset)+24+payloadLen], false) |
| 190 |
} |
| 191 |
if err != nil { |
| 192 |
return -1, fmt.Errorf("extract CBOR: %w", err) |
| 193 |
} |
| 194 |
return offset, nil |
| 195 |
} |
| 196 |
|
| 197 |
func (uswid *UswidSoftwareIdentity) FromJSON(jsonStr string) error { |
| 198 |
if len(jsonStr) == 0 { |
| 199 |
return errors.New("input data empty") |
| 200 |
} |
| 201 |
|
| 202 |
jsonStr = strings.TrimSpace(jsonStr) |
| 203 |
|
| 204 |
if jsonStr[0] == '[' && jsonStr[len(jsonStr)-1] == ']' { |
| 205 |
var uswidID UswidSoftwareIdentity |
| 206 |
if err := json.Unmarshal([]byte(jsonStr), &uswidID.Identities); err != nil { |
| 207 |
return err |
| 208 |
} |
| 209 |
uswid.Identities = append(uswid.Identities, uswidID.Identities...) |
| 210 |
} else { |
| 211 |
var id swid.SoftwareIdentity |
| 212 |
if err := id.FromJSON([]byte(jsonStr)); err != nil { |
| 213 |
return err |
| 214 |
} |
| 215 |
uswid.Identities = append(uswid.Identities, id) |
| 216 |
} |
| 217 |
return nil |
| 218 |
} |
| 219 |
|
| 220 |
func (uswid *UswidSoftwareIdentity) FromXML(xmlStr string) error { |
| 221 |
if len(xmlStr) == 0 { |
| 222 |
return errors.New("input data empty") |
| 223 |
} |
| 224 |
|
| 225 |
var offset int64 = 0 |
| 226 |
for offset < int64(len(xmlStr)) { |
| 227 |
var id swid.SoftwareIdentity |
| 228 |
xmlDecoder := xml.NewDecoder(bytes.NewReader([]byte(xmlStr[offset:]))) |
| 229 |
if err := xmlDecoder.Decode(&id); err != nil { |
| 230 |
if err == io.EOF { |
| 231 |
break |
| 232 |
} |
| 233 |
return err |
| 234 |
} |
| 235 |
uswid.Identities = append(uswid.Identities, id) |
| 236 |
offset += xmlDecoder.InputOffset() |
| 237 |
} |
| 238 |
return nil |
| 239 |
} |
| 240 |
|
| 241 |
func (uswid *UswidSoftwareIdentity) FromPC(pcData string, filename string) error { |
| 242 |
pcLines := strings.Split(pcData, "\n") |
| 243 |
var id swid.SoftwareIdentity |
| 244 |
var softwareMeta swid.SoftwareMeta |
| 245 |
for _, pcLine := range pcLines { |
| 246 |
field := strings.Split(pcLine, ":") |
| 247 |
if (len(field) != 2) { |
| 248 |
continue |
| 249 |
} |
| 250 |
value := strings.TrimSpace(field[1]) |
| 251 |
switch field[0] { |
| 252 |
case "Name": |
| 253 |
id.SoftwareName = value |
| 254 |
case "Description": |
| 255 |
softwareMeta.Summary = value |
| 256 |
case "Version": |
| 257 |
id.SoftwareVersion = value |
| 258 |
} |
| 259 |
} |
| 260 |
id.AddSoftwareMeta(softwareMeta) |
| 261 |
id.TagID = *swid.NewTagID(uuid.NewSHA1(uuid.NameSpaceDNS, []byte(filename))) |
| 262 |
if len(id.Entities) == 0 { |
| 263 |
entity, _ := swid.NewEntity("goswid (auto-generated)", swid.RoleTagCreator) |
| 264 |
id.AddEntity(*entity) |
| 265 |
} |
| 266 |
uswid.Identities = append(uswid.Identities, id) |
| 267 |
return nil |
| 268 |
} |
| 269 |
|
| 270 |
func (uswid UswidSoftwareIdentity) ToUSWID(compress bool) ([]byte, error) { |
| 271 |
var header [16 + 1 + 2 + 4 + 1]byte |
| 272 |
copy(header[:16], magic) |
| 273 |
header[16] = 2 |
| 274 |
binary.LittleEndian.PutUint16(header[17:19], 24) |
| 275 |
|
| 276 |
header[23] = 0x00 |
| 277 |
if compress { |
| 278 |
header[23] |= 0x01 |
| 279 |
} |
| 280 |
cborBuf, err := uswid.ToCBOR(compress) |
| 281 |
if err != nil { |
| 282 |
return nil, err |
| 283 |
} |
| 284 |
binary.LittleEndian.PutUint32(header[19:23], uint32(len(cborBuf))) |
| 285 |
return append(header[:], cborBuf...), nil |
| 286 |
} |
| 287 |
|
| 288 |
func (uswid UswidSoftwareIdentity) ToJSON() ([]byte, error) { |
| 289 |
var jsonBuf []byte |
| 290 |
if len(uswid.Identities) > 1 { |
| 291 |
jsonBuf = append(jsonBuf, '[') |
| 292 |
jsonBuf = append(jsonBuf, ' ') |
| 293 |
} |
| 294 |
for i, id := range uswid.Identities { |
| 295 |
buf, err := id.ToJSON() |
| 296 |
if err != nil { |
| 297 |
return nil, fmt.Errorf("convert to JSON: %w", err) |
| 298 |
} |
| 299 |
jsonBuf = append(jsonBuf, buf...) |
| 300 |
if i < len(uswid.Identities)-1 { |
| 301 |
jsonBuf = append(jsonBuf, ',') |
| 302 |
} |
| 303 |
} |
| 304 |
if len(uswid.Identities) > 1 { |
| 305 |
jsonBuf = append(jsonBuf, ' ') |
| 306 |
jsonBuf = append(jsonBuf, ']') |
| 307 |
} |
| 308 |
return jsonBuf, nil |
| 309 |
} |
| 310 |
|
| 311 |
func (uswid UswidSoftwareIdentity) ToXML() ([]byte, error) { |
| 312 |
var xmlBuf []byte |
| 313 |
for _, id := range uswid.Identities { |
| 314 |
id.XMLName.Space = "http://standards.iso.org/iso/19770/-2/2015/schema.xsd" |
| 315 |
id.XMLName.Local = "SoftwareIdentity" |
| 316 |
|
| 317 |
buf, err := id.ToXML() |
| 318 |
if err != nil { |
| 319 |
return nil, fmt.Errorf("convert to XML: %w", err) |
| 320 |
} |
| 321 |
xmlBuf = append(xmlBuf, buf...) |
| 322 |
} |
| 323 |
return xmlBuf, nil |
| 324 |
} |
| 325 |
|
| 326 |
func (uswid UswidSoftwareIdentity) ToCBOR(compress bool) ([]byte, error) { |
| 327 |
var cborBuf []byte |
| 328 |
for _, id := range uswid.Identities { |
| 329 |
buf, err := id.ToCBOR() |
| 330 |
if err != nil { |
| 331 |
return nil, fmt.Errorf("convert to CBOR: %w", err) |
| 332 |
} |
| 333 |
cborBuf = append(cborBuf, buf...) |
| 334 |
} |
| 335 |
if compress { |
| 336 |
var buf bytes.Buffer |
| 337 |
zlibWriter := zlib.NewWriter(&buf) |
| 338 |
_, err := zlibWriter.Write(cborBuf) |
| 339 |
if err != nil { |
| 340 |
return nil, fmt.Errorf("cannot zlib compress CBOR data: %w", err) |
| 341 |
} |
| 342 |
zlibWriter.Close() |
| 343 |
return buf.Bytes(), nil |
| 344 |
} else { |
| 345 |
return cborBuf, nil |
| 346 |
} |
| 347 |
} |