Skip to content

Commit

Permalink
sign: switch to SHA2-256 signature by default
Browse files Browse the repository at this point in the history
Switch to SHA2-256 signature by default for the `melange sign`
command.

Use the same runtime opt-out back to SHA1 signatures as apko.

With apko from:
- chainguard-dev/apko#1440

This will use RSA256 signature type for both .apk & APKINDEX.tar.gz
signing.
  • Loading branch information
xnox committed Dec 13, 2024
1 parent 004666b commit 3d1b7b3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ lint: checkfmt setup-golangci-lint ## Run linters and checks like golangci-lint
.PHONY: unit
unit:
go test ./... -race
SIGNING_DIGEST=SHA1 go test ./... -race

.PHONY: integration
integration:
go test ./... -race -tags=integration
SIGNING_DIGEST=SHA1 go test ./... -race -tags=integration

.PHONY: test
test: integration
Expand Down
20 changes: 15 additions & 5 deletions pkg/build/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ type ApkSigner interface {
SignatureName() string
}

var melangeApkDigest crypto.Hash

func init() {
melangeApkDigest = crypto.SHA256
if digest, ok := os.LookupEnv("SIGNING_DIGEST"); ok {
switch digest {
case "SHA256":
case "SHA1":
melangeApkDigest = crypto.SHA1
default:
panic(fmt.Errorf("unsupported SIGNING_DIGEST"))
}
}
}

func EmitSignature(ctx context.Context, signer ApkSigner, controlData []byte, sde time.Time) ([]byte, error) {
_, span := otel.Tracer("melange").Start(ctx, "EmitSignature")
defer span.End()
Expand Down Expand Up @@ -73,12 +88,7 @@ type KeyApkSigner struct {
KeyPassphrase string
}

const melangeApkDigest = crypto.SHA1

// const melangeApkDigest = crypto.SHA256

func (s KeyApkSigner) Sign(control []byte) ([]byte, error) {

controlDigest, err := sign.HashData(control, melangeApkDigest)
if err != nil {
return nil, err
Expand Down
16 changes: 12 additions & 4 deletions pkg/sign/apk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ func TestAPK(t *testing.T) {
if err != nil {
t.Fatal(err)
}
melangeApkDigest := crypto.SHA1
prefix := ".SIGN.RSA."
// melangeApkDigest := crypto.SHA256
// prefix := ".SIGN.RSA256."
melangeApkDigest := crypto.SHA256
prefix := ".SIGN.RSA256."
if digest, ok := os.LookupEnv("SIGNING_DIGEST"); ok {
switch digest {
case "SHA256":
case "SHA1":
melangeApkDigest = crypto.SHA1
prefix = ".SIGN.RSA."
default:
t.Fatalf("unsupported SIGNING_DIGEST")
}
}
if sigName != prefix+testPubkey {
t.Fatalf("unexpected signature name %s", sigName)
}
Expand Down

0 comments on commit 3d1b7b3

Please sign in to comment.