Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apk/signature: remove support for creating new SHA1 signatures #1496

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/apk/signature/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ var (
errDigestLength = errors.New("digest has unexpected length")
errNoPassphrase = errors.New("key is encrypted but no passphrase was provided")
errNoRSAKey = errors.New("key is not an RSA key")
errWeakDigest = errors.New("creating sha1 signatures not supported")
)

// RSASignDigest signs the provided message digest. The key file must
// be in the PEM format and can either be encrypted or not.
func RSASignDigest(digest []byte, digestType crypto.Hash, keyFile, passphrase string) ([]byte, error) {
if digestType == crypto.SHA1 {
return nil, errWeakDigest
}
if len(digest) != digestType.Size() {
return nil, errDigestLength
}
Expand Down
37 changes: 4 additions & 33 deletions pkg/apk/signature/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,54 +46,25 @@ func SignIndex(ctx context.Context, signingKey string, indexFile string) error {

log.Infof("signing index %s with key %s", indexFile, signingKey)

// Unfortunately apk-tools checks signatures until the first one passes, and skips the rest.
// And golang sorts its reads & writes in lexical order only. Thus indexes will be
// validated with SHA1 only by apk-tools whilst RSA (SHA1) signature is present.
//
// An incremental WriteTargz would allow to write out strongest hash first. Or a MergeFS
// implementation the maintains relative order.
//
// Done:
// Step 0) apk-tools supports RSA256 since 2017
// Step 1) Upgrade all deployments of melange/go-apk with verification support for RSA256
// This PR:
// Step 2) Turn on RSA256 signatures, with RSA escape hatch
// Next:
// Step 3) Remove RSA escape hatch

filename := "RSA256"
digestType := crypto.SHA256

if digest, ok := os.LookupEnv("SIGNING_DIGEST"); ok {
switch digest {
case "SHA256":
case "SHA1":
filename = "RSA"
digestType = crypto.SHA1
default:
return fmt.Errorf("unsupported SIGNING_DIGEST")
}
}

indexData, err := os.ReadFile(indexFile)
if err != nil {
return fmt.Errorf("unable to read index for signing: %w", err)
}

sigFS := memfs.New()
indexDigest, err := HashData(indexData, digestType)
indexDigest, err := HashData(indexData, crypto.SHA256)
if err != nil {
return err
}

sigData, err := RSASignDigest(indexDigest, digestType, signingKey, "")
sigData, err := RSASignDigest(indexDigest, crypto.SHA256, signingKey, "")
if err != nil {
return fmt.Errorf("unable to sign index: %w", err)
}

log.Infof("appending signature %s to index %s", filename, indexFile)
log.Infof("appending signature RSA256 to index %s", indexFile)

if err := sigFS.WriteFile(fmt.Sprintf(".SIGN.%s.%s.pub", filename, filepath.Base(signingKey)), sigData, 0644); err != nil {
if err := sigFS.WriteFile(fmt.Sprintf(".SIGN.RSA256.%s.pub", filepath.Base(signingKey)), sigData, 0644); err != nil {
return fmt.Errorf("unable to append signature: %w", err)
}

Expand Down
Loading