Skip to content

Commit be22ee7

Browse files
Merge pull request #381 from viveksahu26/fix/signature_field_msg
fix signature field msg
2 parents 97b9091 + 175c478 commit be22ee7

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

pkg/compliance/bsiV2.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package compliance
1616

1717
import (
1818
"context"
19+
"os"
1920
"strings"
2021

2122
"github.com/interlynk-io/sbomqs/pkg/compliance/common"
@@ -105,7 +106,13 @@ func bsiV2SbomSignature(doc sbom.Document) *db.Record {
105106
pubKey := doc.Signature().GetPublicKey()
106107
blob := doc.Signature().GetBlob()
107108
sig := doc.Signature().GetSigValue()
108-
valid, err := common.VerifySignature(pubKey, blob, sig)
109+
110+
pubKeyData, err := os.ReadFile(pubKey)
111+
if err != nil {
112+
return db.NewRecordStmt(SBOM_SIGNATURE, "doc", "Sig not detected!", 0.0, "")
113+
}
114+
115+
valid, err := common.VerifySignature(pubKeyData, blob, sig)
109116
if err != nil {
110117
return db.NewRecordStmt(SBOM_SIGNATURE, "doc", "Verification failed!", 0.0, "")
111118
}
@@ -114,7 +121,7 @@ func bsiV2SbomSignature(doc sbom.Document) *db.Record {
114121
result = "Signature verification succeeded!"
115122
} else {
116123
score = 5.0
117-
result = "Signature verification failed!"
124+
result = "Signature provided but verification failed!"
118125
}
119126

120127
common.RemoveFileIfExists("extracted_public_key.pem")

pkg/compliance/common/common.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,7 @@ func AreLicensesValid(licenses []licenses.License) bool {
489489
return spdx+aboutcode+custom == len(licenses)
490490
}
491491

492-
func VerifySignature(publicKeyPath, sbomPath, signaturePath string) (bool, error) {
493-
pubKeyData, err := os.ReadFile(publicKeyPath)
494-
if err != nil {
495-
return false, err
496-
}
497-
492+
func VerifySignature(pubKeyData []byte, sbomPath, signaturePath string) (bool, error) {
498493
block, _ := pem.Decode(pubKeyData)
499494
if block == nil || block.Type != "PUBLIC KEY" {
500495
return false, fmt.Errorf("invalid public key")

pkg/sbom/cdx.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/google/uuid"
3232
"github.com/interlynk-io/sbomqs/pkg/cpe"
3333
"github.com/interlynk-io/sbomqs/pkg/licenses"
34+
"github.com/interlynk-io/sbomqs/pkg/logger"
3435
"github.com/interlynk-io/sbomqs/pkg/omniborid"
3536
"github.com/interlynk-io/sbomqs/pkg/purl"
3637
"github.com/interlynk-io/sbomqs/pkg/swhid"
@@ -170,7 +171,7 @@ func (c *CdxDoc) parse() {
170171
c.parsePrimaryCompAndRelationships()
171172
c.parseVulnerabilities()
172173
if c.Signature().GetSigValue() == "" && c.Signature().GetPublicKey() == "" {
173-
fmt.Println("Extract public key and signature from SBOM")
174+
c.addToLogs("extract public key and signature from cylonedx sbom itself")
174175
c.parseSignature()
175176
}
176177
c.parseComps()
@@ -255,6 +256,8 @@ func (c *CdxDoc) parseVulnerabilities() {
255256
// until and unless cyclondx-go library supports signature, this part is useless
256257
// So, we are using tech hack to parse signature directly from JSON sbom file
257258
func (c *CdxDoc) parseSignature() {
259+
log := logger.FromContext(c.ctx)
260+
log.Debug("parseSignature()")
258261
c.SignatureDetail = &Signature{}
259262
if c.doc.Declarations != nil {
260263
if c.doc.Declarations.Signature != nil {
@@ -265,27 +268,27 @@ func (c *CdxDoc) parseSignature() {
265268
// decode the signature
266269
signatureValue, err := base64.StdEncoding.DecodeString(sigValue)
267270
if err != nil {
268-
fmt.Println("Error decoding signature:", err)
271+
log.Debug("Error decoding signature:", err)
269272
return
270273
}
271274

272275
// write the signature to a file
273276
if err := os.WriteFile("extracted_signature.bin", signatureValue, 0o600); err != nil {
274-
fmt.Println("Error writing signature to file:", err)
277+
log.Debug("Error writing signature to file: %s", err)
275278
return
276279
}
277280
c.addToLogs("Signature written to file: extracted_signature.bin")
278281

279282
// extract the public key modulus and exponent
280283
modulus, err := base64.StdEncoding.DecodeString(pubKeyModulus)
281284
if err != nil {
282-
fmt.Println("Error decoding public key modulus:", err)
285+
log.Debug("Error decoding public key modulus:", err)
283286
return
284287
}
285288

286289
exponent := decodeBase64URLEncodingToInt(pubKeyExponent)
287290
if exponent == 0 {
288-
fmt.Println("Invalid public key exponent.")
291+
c.addToLogs("Invalid public key exponent.")
289292
return
290293
}
291294

@@ -298,7 +301,7 @@ func (c *CdxDoc) parseSignature() {
298301
// write the public key to a PEM file
299302
pubKeyPEM := publicKeyToPEM(pubKey)
300303
if err := os.WriteFile("extracted_public_key.pem", pubKeyPEM, 0o600); err != nil {
301-
fmt.Println("Error writing public key to file:", err)
304+
log.Debug("Error writing public key to file:", err)
302305
return
303306
}
304307

0 commit comments

Comments
 (0)